| by Arround The Web | No comments

Python Insert Character Into String

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:

string = "Hello!"

After that, use another variable for the character(s) to be added:

to_Add = “GG”

Concatenate the two string variable and store the result in the first one:

string = string + to_Add

Print the result on the terminal using the following line:

print(string)

The complete code snippet:

string = "Hello!"

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:

string = " Hello!"

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:

string1 = "Hello! LinuxHint!"

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:

string1 = string1[:7] + to_add + string1[7:]

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:

print(string1)

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.

Share Button

Source: linuxhint.com

Leave a Reply