• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
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.  Using Lambda Expression with max() in Python
  • 2.  Get Last Day of Month Using Python
  • 3.  How to Measure Execution Time of Program in Python
  • 4.  Print Time Elapsed in Python
  • 5.  pandas Drop Columns – Delete Columns from a DataFrame
  • 6.  pandas Duplicated – Find Duplicate Rows in DataFrame or Series
  • 7.  Find Index of Minimum in List Using Python
  • 8.  Selenium maximize_window() Function to Maximize Window in Python
  • 9.  Get Days in Month Using Python
  • 10.  Python Destroy Object – How to Delete Objects with del Keyword

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 *

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