• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Add String to List

Using Python to Add String to List

August 22, 2022 Leave a Comment

To add a string to a list in Python, the easiest way is to use the Python list append() function.

string = "string"
lst = ["a", "b", "c"]

lst.append(string)

print(lst)

#Output:
["a", "b", "c", "string"]

You can also wrap a string in square brackets and use the concatenation operator + to add a string to a list.

string = "string"
lst = ["a", "b", "c"]

lst = lst + [string]

print(lst)

#Output:
["a", "b", "c", "string"]

When working with collections of data, the ability to add and remove items from your collection easily is very valuable.

One such case is if you want to add a string to a lits of objects in Python.

To add a string to a list in Python, the easiest way is to use the Python list append() function.

Below is a simple example showing you how to add a string to a list in Python.

string = "string"
lst = ["a", "b", "c"]

lst.append(string)

print(lst)

#Output:
["a", "b", "c", "string"]

Using + operator to Add String to List in Python

Another way you can add a string variable to a list is with the Python concatenation operator +.

To use +, you should wrap the string variable in square brackets to convert it to a list and then add it to a list as shown below.

string = "string"
lst = ["a", "b", "c"]

lst = lst + [string]

print(lst)

#Output:
["a", "b", "c", "string"]

Hopefully this article has been useful for you to learn how to add a string to a list in Python.

Other Articles You'll Also Like:

  • 1.  Factorial Program in Python Using For Loop and While Loop
  • 2.  Python Prime Factorization – Find Prime Factors of Number
  • 3.  Python Check if Dictionary Value is Empty
  • 4.  Get Substring Between Two Characters with Python
  • 5.  How to Filter Lists in Python
  • 6.  How to Check if Set is Empty in Python
  • 7.  Find Index of Minimum in List Using Python
  • 8.  Using Python turtle Module to Draw Square
  • 9.  Count Number of Keys in Dictionary in Python
  • 10.  Using Python to Add Trailing Zeros to String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy