To append a character to a string in Python, the easiest way is with the + operator.
string = "this is a string"
character = "."
string_appended = string + character
print(string_appended)
#Output:
this is a string.
When working with strings in Python, the ability to easily be able to modify the values of these variables is valuable.
One such case is if you want to append characters to a string variable and add characters at the end of the string.
To append a character to a string in Python, the easiest way is with the + operator. + concatenates two strings together and therefore we can use it to append a character to a string.
Below is an example showing you how to append a character to a string in Python with +.
string = "this is a string"
character = "."
string_appended = string + character
print(string_appended)
#Output:
this is a string.
How to Prepend Character to String in Python
If you want to add a character at the beginning of a string, instead of the end of a string, you can modify the example from above.
To prepend a character to a string variable, you can use + and switch the order from above.
Below is an example showing you how to prepend a character to a string in Python with +.
string = "this is a string"
character = "."
string_prepended = character + string
print(string_prepended)
#Output:
.this is a string
Hopefully this article has been useful for you to learn how to append a character to a string variable in Python.