• 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.  Python not in – Check if Value is Not Included in Object
  • 2.  Not Equal Operator != in Python
  • 3.  How to Check if String Contains Lowercase Letters in Python
  • 4.  Python Remove First Element from List
  • 5.  Python Factorial Recursion – Using Recursive Function to Find Factorials
  • 6.  Generate Random Float Between 0 and 1 Using Python
  • 7.  How to Save and Use Styles from Existing Word File With python-docx
  • 8.  Drop Last n Rows of pandas DataFrame
  • 9.  Fibonacci Sequence in Python with for Loop
  • 10.  Find Maximum of Three Numbers in Python

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

x