last modified July 29, 2022
Python add string tutorial shows how to concatenate strings in Python.
Python tutorial is a comprehensive tutorial on Python language.
In Python, a string is an ordered sequence of Unicode characters.
There are several ways of adding strings in Python:
- + operator
- __add__ method
- join method
- formatting strings
Python add strings with + operator
The easiest way of concatenating strings is to use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded.
add_string.py
#!/usr/bin/python a = 'old' b = ' tree' c = a + b print(c)Two strings are added using the + operator.
$ ./add_string.py old treeIn the second example, we use the compound addition operator.
add_string2.py
The example builds a message with the += operator.
$ ./add_string2.py There are three falcons in the skyPython add strings with join
The string join method concatenates any number of strings provided in an iterable (tuple, list). We specify the character by which the strings are joined.
add_string_join.py
#!/usr/bin/python msg = ' '.join(['There', 'are', 'three', 'eagles', 'in', 'the', 'sky']) print(msg)In the example, we form a message by joining seven words. The words are joined with a single space character.
$ ./add_string_join.py There are three eagles in the skyPython add strings with string formatting
We can build Python strings with string formatting. The variables are expanded in the {} characters inside the string.
format_str.py
#!/usr/bin/python w1 = 'two' w2 = 'eagles' msg = f'There are {w1} {w2} in the sky' print(msg)We build a message with Python's fstring.
$ ./format_str.py There are two eagles in the skyPython add strings with __add_ method
Another possibility to add strings is to use the special __add__ dunder method.
add_string3.py
#!/usr/bin/python s1 = "and old" s2 = " falcon" s3 = s1.__add__(s2) print(s3)The example adds two strings with __add__.
In this tutorial, we have shown several ways to add strings in Python.
Read Python tutorial or list all Python tutorials.
Introduction
In this tutorial, we will learn different ways to add elements to a list in Python.
There are four methods to add elements to a List in Python.
- append(): append the element to the end of the list.
- insert(): inserts the element before the given index.
- extend(): extends the list by appending elements from the iterable.
- List Concatenation: We can use the + operator to concatenate multiple lists and create a new list.
append()
This function adds a single element to the end of the list.
fruit_list = ["Apple", "Banana"] print(f'Current Fruits List {fruit_list}') new_fruit = input("Please enter a fruit name:\n") fruit_list.append(new_fruit) print(f'Updated Fruits List {fruit_list}')Output:
Current Fruits List ['Apple', 'Banana'] Please enter a fruit name: Orange Updated Fruits List ['Apple', 'Banana', 'Orange']This example added Orange to the end of the list.
insert()
This function adds an element at the given index of the list.
num_list = [1, 2, 3, 4, 5] print(f'Current Numbers List {num_list}') num = int(input("Please enter a number to add to list:\n")) index = int(input(f'Please enter the index between 0 and {len(num_list) - 1} to add the number:\n')) num_list.insert(index, num) print(f'Updated Numbers List {num_list}')Output:
This example added 20 at the index of 2. 20 has been inserted into the list at this index.
extend()
This function adds iterable elements to the list.
extend_list = [] extend_list.extend([1, 2]) # extending list elements print(extend_list) extend_list.extend((3, 4)) # extending tuple elements print(extend_list) extend_list.extend("ABC") # extending string elements print(extend_list)Output:
[1, 2] [1, 2, 3, 4] [1, 2, 3, 4, 'A', 'B', 'C']This example added a list of [1, 2]. Then it added a tuple of (3, 4). And then it added a string of ABC.
List Concatenation
If you have to concatenate multiple lists, you can use the + operator. This will create a new list, and the original lists will remain unchanged.
evens = [2, 4, 6] odds = [1, 3, 5] nums = odds + evens print(nums) # [1, 3, 5, 2, 4, 6]This example added the list of evens to the end of the list of odds. The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.
Conclusion
Python provides multiple ways to add elements to a list. We can append an element at the end of the list, and insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded + operator
References:
- Python List
- Python.org Docs