In this Python tutorial, we will see how to add characters to an empty string in Python with various methods using demonstrative examples.
In Python, a string is a sequence of characters enclosed in either single quotes (‘ ‘) or double quotes (” “). We can assign a string to a variable like this:
my_string = 'Hello, World!'
We can create an empty string by assigning no characters between the quotes, like this:
empty_string = ''"
Add characters to an empty string in Python
Adding characters to an empty string in Python can be accomplished using many methods. They are:
- + or += Operator
- join() Method
- format() Method
- f-strings
- str() Function
- % Operator
Let’s see them one by one using illustrative examples.
Method-1: Add characters to an empty string in Python using + or += Operator
The most common method of adding characters to an empty string in Python is by using the +
or += operator. This operator combines two strings into one.
Consider we’re visiting the Grand Canyon in Arizona. We want to create a string that says, “Visiting the Grand Canyon in Arizona”. We have an empty string called Sentence and we have to add these character that only says “Visiting the”, ” Grand Canyon” and “in Arizona” to it.
Let’s try to add these characters using Python + operator first.
Sentence = ""
Sentence = Sentence + 'Visiting the' + ' ' + 'Grand Canyon' + ' ' + 'in Arizona'
print(Sentence)
The Output is:
In this case:
- An empty string is created and assigned to the variable Sentence.
- The Python + operator concatenates the empty string with the characters, and the result is reassigned to Sentence.
- ThePython print() function is then used to output the value of Sentence, which is now:
Visiting the Grand Canyon in Arizona
Note: As we can see the variable(Sentence) is coming twice in one of the line in our code. Being a Python programmer, we should avoid code repetition in our program. So, we can use += operator in the above example to avoid repetition.
In the above example, we can change:
Sentence = ""
Sentence += 'Visiting the' + ' ' + 'Grand Canyon' + ' ' + 'in Arizona'
print(Sentence)
The Output: Same as the first one
Visiting the Grand Canyon in Arizona
This way, we can use the + or += operator to add the characters to an empty string in Python.
Method-2: Append characters to an empty string in Python using the join() method
Another approach to append characters or elements to a string in Python is by using the join() method. This method combines a list of strings into a single string with a specified delimiter.
Let’s use the names of the five largest states by population in the USA as an example.
all_states = ''
states = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania']
all_states += ', '.join(states)
print(all_states)
The Output is: In the above example, we start with a string in Python, a list of state names, then use the join() method to append to the empty string, with each state name separated by a comma and a space.
California, Texas, Florida, New York, Pennsylvania
This way, we can use the join() method to append characters to an empty string in Python.
Method-3: Add elements to the string in Python using format() method
Python’s format() method can also be used to add characters to an empty string. The method allows us to interpolate specified values into a Python string, which can be particularly useful when constructing complex sentences or messages.
Here’s an example using some of the tallest buildings in the USA:
message = ''
building1 = 'One World Trade Center'
building2 = 'Central Park Tower'
building3 = 'Willis Tower'
message += 'The tallest buildings in the USA include {}, {}, and {}.'.format(building1, building2, building3)
print(message)
The output is:
The tallest buildings in the USA include One World Trade Center, Central Park Tower, and Willis Tower.
This way, we can use the format() method to add elements to the string in Python.
Method-4: Append characters to empty string in Python using f-strings
Since Python 3.6, we have f-strings, a new way to format strings in Python. They are easy to use and read, making them a great string formatting tool. This can be used to append characters to an empty string in Python.
Let’s use the names of some famous American inventors as an example:
message = ''
inventor1 = 'Thomas Edison'
inventor2 = 'Alexander Graham Bell'
inventor3 = 'Nikola Tesla'
message += f'Famous American inventors include {inventor1}, {inventor2}, and {inventor3}.'
print(message)
The Output is:
Famous American inventors include Thomas Edison, Alexander Graham Bell, and Nikola Tesla.
This way, we can append characters to empty string in Python using f-strings.
Method-5: Append a letter to a string in python using the str() function
In Python, the str() is a built-in function that converts non-string data types to a string.
Let’s consider the numbers of some major U.S. highways: In this example, the Python str() function is used to convert integer values to strings before concatenating them to other strings.
highway1 = 66
highway2 = 101
highway3 = 61
message = "Major U.S. highways include " + str(highway1) + ", " + str(highway2) + ", and " + str(highway3) + "."
print(message)
The output is:
Major U.S. highways include 66, 101, and 61.
This way we can use the str() function to append a letter to a string in Python.
Method-6: Add value to empty string in Python using the % operator
Python also supports C-style string formatting, where substitutions are made using the % operator and a format specifier. Here, we are working on the Python string. So, we need %s as a format specifier.
Let’s try to put the city, state, and, its population in one empty string.
message = ''
city = "San Francisco"
state = "California"
population = 883305
message += "The city of %s is in the state of %s and it has a population of %d." % (city, state, population)
print(message)
The output is:
The city of San Francisco is in the state of California and it has a population of 883305.
This way, we can use the % operator to add value to an empty string in Python.
Append character to a string python
Now, we will see how to append a character to a string in Python.
- To append a character to a string, we will insert the character at an index of a string, and it will create a new string with that character.
- To insert a character, we will use slicing a_str[:1] + “n” + a_str[1:], and the “+” operator is used to insert the desired character.
Example:
a_str = "Hello"
a_str = a_str[:1] + "n" + a_str[1:]
print(a_str)
You can refer to the below screenshot to see the output for append character to a string Python
This is how to append a character to a string in Python.
Add a letter to a string Python
Here, we will see how to add a letter to a string Python.
To add a letter to a string python we will use f”{str1}{l2}”, and to get the output, we will print(res).
Example:
str1 = "Python"
l2 = "G"
res = f"{str1}{l2}"
print(res)
You can refer to the below screenshot to see the output for adding a letter to a string Python.
This is Python code to add a letter to a string in Python.
How to append to an empty string in Python
Let’s see how to append to an empty string in Python.
To append to an empty string in Python, we have to use the “+” operator to append in an empty string.
Example:
str = ""
res = str + "Hello World"
print(res)
You can refer to the screenshot below to see the output for appending to an empty Python string.
The above code we can use to append to an empty string in Python.
Conclusion
Adding characters to an empty string in Python is straightforward. Whether you’re concatenating strings using the “+” or “+=” operators or “%” operator or combining a list of strings with the join() method, format() method, using f-string, str() function. Python offers flexible options to suit our needs.
You may also like:
- Add a string to a list in Python
- convert tuple to comma separated string in Python
- Remove a specific character from string in Python
- How to remove the last character from a string in Python
- Add two numbers in Python using the function
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.