How to Add Characters to a String in Python?

In this tutorial, I will explain how to add characters to a string in Python. As a software developer working for an e-commerce platform, I encountered a situation where I needed to format product IDs by appending prefixes and suffixes dynamically. After researching different methods, I found several effective solutions that I will share with you in this article. I will share my findings and provide detailed examples.

Strings in Python

Strings in Python are immutable, meaning that once a string is created, its contents cannot be changed. However, you can create new strings based on existing ones by adding characters or concatenating them.

Read How to Escape Curly Braces in Python Format Strings?

Add Characters to a String in Python

This guide will cover various methods to achieve this in Python, including using the + operator, the join() method, and string formatting techniques.

1. Use the + Operator

One of the simplest ways to add characters to a string is by using the + operator. This operator allows you to concatenate two or more strings together. For example, let’s say you have a string representing a city name and you want to add the state abbreviation:

city = "Los Angeles"
state = "CA"
location = city + ", " + state
print(location) 

Output:

 Los Angeles, CA

I have executed the above example code and added the screenshot below.

Add Characters to a String in Python

In this example, we combined the city and state into a single string, demonstrating how easy it is to append characters.

Read How to Parse a String in Python?

2. Insert Characters at a Specific Position

If you need to insert a character or substring at a specific position within a Python string, you can achieve this by slicing the string. Here’s how you can do it:

original_string = "JohnDoe"
insert_position = 4
new_character = " "
modified_string = original_string[:insert_position] + new_character + original_string[insert_position:]
print(modified_string)

Output:

John Doe

I have executed the above example code and added the screenshot below.

How to Add Characters to a String in Python

In this case, we inserted a space between “John” and “Doe” by slicing the original string at the desired position.

Read How to Add Line Breaks in Python Strings?

3. Use the join() Method to Add Characters

Python join() method is particularly useful when you want to add a character between each element of a list. For instance, if you have a list of names and you want to create a single string with commas separating them, you can do the following:

names = ["Alice", "Bob", "Charlie"]
formatted_names = ", ".join(names)
print(formatted_names)  

Output:

Alice, Bob, Charlie

I have executed the above example code and added the screenshot below.

Add Characters to a String in Python join()

This method is efficient and clean, especially when dealing with multiple strings.

Read How to Remove Commas from a String in Python?

4. Format Strings with f-Strings

Python’s f-strings (formatted string literals) allow you to embed expressions inside string literals. This can be particularly useful when you want to construct a string that includes variables. Here’s an example:

first_name = "Michael"
last_name = "Smith"
full_name = f"{first_name} {last_name}"
print(full_name)  # Output: Michael Smith

Using f-strings not only makes your code more readable but also allows for easy manipulation of string contents.

Read How to Continue Long Strings on the Next Line in Python?

Conclusion

In this tutorial, I have explained how to add characters to a string in Python. I discussed methods like the + operator, inserting characters at a specific position, using the join() method, and format string with f-string.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.