Working with strings is something that every developer needs to be a master of. Because, when you are developing a Program, or an application and the user interacts with it and fills in the details. Most of the time, the details are in the form of a string.
Strings cannot be changed after they have been initialized in Python, however, if you want to add characters to a string, then there are various methods to do this.
How to Insert Character(s) at the Start or End of a String?
If the task is to add the characters at the end or at the start of a string then that can be done by using concatenation in Python. Concatenation simply means to adjoin multiple strings. To perform concatenation in python, the plus symbol “+” is used.
Example
Create a string and store it inside a variable:
After that, use another variable for the character(s) to be added:
Concatenate the two string variable and store the result in the first one:
Print the result on the terminal using the following line:
The complete code snippet:
to_Add = "GG"
string = string + to_Add
print(string)
When this code is executed, the following result will be displayed:
And if the characters are to be added at the start of the string, then simply change the order of the concatenation:
to_Add = "GG"
string = to_Add + string
print(string)
The will produce the following result:
The output confirms that the characters have been added at the start of the string.
How to Insert Character(s) in the Middle of a String?
If you want to add some characters in the middle of the string then that cannot be done by using the concatenation operator. In this case, the user has to split the string by either using the string slicing or the rsplit() method and then use the concatenation operator to merge the parts of the string.
To demonstrate this, create a string variable and also the characters that are to be added in the middle of the string:
to_add = "This is "
The task is to hand the string “to_Add” right after “Hello!” and before “LinuxHint”, to do that use the following line:
In this line:
- One part of a string was made from the start to the 7th index which is the character “ “ (blank space).
- Then the “to_Add” variable was concatenated after the first partition.
- And then, the second part of the string was added at the end of the string starting from index “7” to the very end
At the end, print out the “string1” using the following line of code:
This will produce the following result on the terminal:
This output confirms that the characters have been successfully added in the middle of the string.
Conclusion
Strings, by default, are not editable, this means that they are not changeable, not editable, or not modifiable after their creation. However, with the help of concatenation, character(s) can easily be added to the string. The methods for achieving this task are also pretty easy. If the task is to add character(s) at either end of the string then simply use the concatenation operator (+), and if the task is to add them in the middle, then split the string and add the character(s) using the concatenation operator.