To append a character or another string to a string in Python, use +=: word += "n". Python strings can’t be changed in place, so there is no append() method; + and += build a new string. To add to the beginning, put the new text first ("#" + s), to insert in the middle, slice the string, and to combine many pieces, use "".join().
word = "Pytho"
word += "n" # append a character
print(word)
Output:
Python
Every example was run with Python 3.12.5; the output shown is from those runs.
Append with + and +=
greeting = "Hello"
name = "Ann"
message = greeting + ", " + name + "!" # + joins strings into a new one
print(message)
Output:
Hello, Ann!
Add characters to an empty string
Start with "" and add to it in a loop:
result = "" # start with an empty string
for ch in "abc":
result += ch.upper() # add one character at a time
print(result, len(result))
Output:
ABC 3
To test whether a string is still empty, see check if a string is empty in Python.
Add a character to the beginning (prepend)
number = "42"
print("#" + number) # add to the beginning
print(f"ID-{number}") # or with an f-string
Output:
#42
ID-42
Insert a character at a specific position
Slice the string at the position and put the new character between the two parts:
code = "PY312"
position = 2
new_code = code[:position] + "-" + code[position:] # insert "-" at index 2
print(new_code)
Output:
PY-312
More examples: insert a character into a string at an index.
Build a string with f-strings and format()
city = "Boston"
temp = 21.456
print(f"{city}: {temp:.1f}°C") # f-string (Python 3.6+)
print("{}: {:.1f}°C".format(city, temp)) # str.format()
print("%s: %.1f°C" % (city, temp)) # older % formatting
Output:
Boston: 21.5°C
Boston: 21.5°C
Boston: 21.5°C
Join many pieces with join()
letters = ["P", "y", "t", "h", "o", "n"]
print("".join(letters)) # glue a list of strings together
print("-".join(letters)) # with a separator between each
print(", ".join(["red", "green", "blue"]))
Output:
Python
P-y-t-h-o-n
red, green, blue
See also concatenate a list of strings into one string.
Add a separator every N characters
digits = "1234567890"
grouped = " ".join(digits[i:i + 3] for i in range(0, len(digits), 3))
print(grouped) # add a space after every 3 characters
Output:
123 456 789 0
Repeat and pad characters
print("ab" * 3) # repeat a string
print("Title".center(15, "*")) # add characters on both sides
print("7".zfill(3), "7".rjust(3, "0"), "7".ljust(3, "."))
Output:
ababab
*****Title*****
007 007 7..
More in create a string of N characters.
Add characters by their code with chr()
s = ""
for code in range(ord("a"), ord("e") + 1):
s += chr(code) # add characters by their Unicode code
print(s)
Output:
abcde
Why strings have no append()
Strings are immutable: once created, they never change. += looks like it changes the string, but it creates a new object and points the name at it. The original is untouched:
s = "cat"
original = s # keep a second name for the original string
s += "s" # builds a NEW string and points s at it
print(s, original, "| same object?", s is original)
Output:
cats cat | same object? False
That’s why the list-style methods fail:
s = "abc"
s.append("d")
Output:
AttributeError: 'str' object has no attribute 'append'
s = "abc"
s[0] = "X"
Output:
TypeError: 'str' object does not support item assignment
TypeError: can only concatenate str to str
count = 3
print("Items: " + count)
Output:
TypeError: can only concatenate str (not "int") to str
Convert the other value first, or use an f-string:
count = 3
print("Items: " + str(count))
print(f"Items: {count}")
Output:
Items: 3
Items: 3
Fastest way to build a long string
import timeit
from io import StringIO
N = 100_000
def plus_equals():
s = ""
for _ in range(N):
s += "x"
return s
def join_list():
parts = []
for _ in range(N):
parts.append("x")
return "".join(parts)
def string_io():
buf = StringIO()
for _ in range(N):
buf.write("x")
return buf.getvalue()
assert plus_equals() == join_list() == string_io()
for f in (plus_equals, join_list, string_io):
t = min(timeit.repeat(f, number=1, repeat=5))
print(f"{f.__name__:12} {t * 1000:6.1f} ms")
Output (building a 100,000-character string one character at a time):
plus_equals 7.8 ms
join_list 3.4 ms
string_io 4.0 ms
In this run, += took 7.8 ms, a list plus join() 3.4 ms and StringIO 4.0 ms. CPython speeds up s += x when nothing else refers to the string, so it is often fine in practice. That trick stops working as soon as another name points to the same string, because Python must then keep the old value:
a = "x"
b = a # a second name for the same string
for _ in range(3):
a += "y" # b still refers to the old "x", so a must be copied
print(a, b)
Output:
xyyy x
For big loops, collecting the pieces in a list and calling "".join() once is the reliable, recommended way (see str.join() in the Python documentation).
Next, try these string tutorials:
- Insert a character into a string at a specific index
- Concatenate a list of strings into a single string
- Remove a character from a string in Python
- Check if a string is empty in Python
Frequently asked questions
How do I append a character to a string in Python?
Use s += "x" (or s = s + "x"). It creates a new string with the character at the end.
Does Python have a string append() method?
No. Strings are immutable, so "abc".append("d") raises AttributeError. Use +=, or collect pieces in a list and join() them.
How do I add a character to the beginning of a string?
Put it first: s = "#" + s, or f"#{s}".
How do I insert a character in the middle of a string?
Slice and rebuild: s[:i] + "x" + s[i:] inserts "x" at index i.
How do I add to an empty string in a loop?
Start with result = "" and use result += piece, or append to a list and call "".join(parts) at the end.
Why do I get “can only concatenate str (not int) to str”?
You added a number to a string. Convert it with str(n) or use an f-string: f"Items: {n}".
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn