Add Character to Specific Position in String Using Python



In this article, we will learn how to add a character to a specific position in a string in Python.

Methods Used

The following are the various methods to accomplish this task ?

  • Using the '+' operator

  • Using join() function

  • Using format() function

Method 1: Using the '+' operator

We can concatenate strings and characters using the symbol "+" at the required positions.

Adding Character at the End of a String

Algorithm:

Following are the Algorithms/steps to be followed to perform the desired task. ?

  • Create a variable to store an input string.

  • Use the "+" operator to add a character at the end of an input string.

  • Print the resultant string.

You can also add a character at the specific position and starting of a string by using indexing with the "+" operator as shown in the following examples.

Example

The following program adds a character at the end of an input string using the ?+' operator ?

Open Compiler
# input string inputString = "tutorialspoin" # adding a character at the end of an input string using the + operator inputString = inputString + 't' # printing the resultant string print("Adding 't' at the end of an input string:\n", inputString)

Output

On execution, the above program will generate the following output ?

Adding 't' at the end of an input string:
 tutorialspoint

Adding Character at a Specific Position

Example

The following program adds a character at the specific position of an input string using the '+' operator ?

Open Compiler
# input string inputString = "tutorialsoint" # adding character 'p' at a specific position i.e, at index 9 inputString = inputString[:9] + 'p' + inputString[9:] # printing the resultant string print("Adding 'p' at 9th index of an input string:\n", inputString)

Output

Adding 'p' at 9th index of an input string:
 tutorialspoint

Adding Character at Starting Position of a String

Example

The following program adds a character at the start of an input string using the '+' operator ?

Open Compiler
# input string inputString = "utorialspoint" # adding character 't' at the start of an input string inputString = 't' + inputString # printing the resultant string print("Adding 't' at the starting of an input string:\n", inputString)

Output

Adding 't' at the starting of an input string:
 tutorialspoint

Method 2: Using the join() function

Using the join() function in Python, it is also feasible to concatenate characters at required positions inside a string. You can insert characters wherever in the string, whether it's in the middle, the beginning, or the end.

Algorithm 

Following are the Algorithms/steps to be followed to perform the desired task. ?

  • Use the join() function to join a character at the end of an input string.

  • join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to form a string.

  • Print the resultant string.

In the same way, join the characters at the specific position and starting of a string using indexing.

Example

The following program adds a character at the end, a specific position, and the start of an input string using the join() function ?

Open Compiler
# input string 1 inputString_1 = "tutorialspoin" # joining a character at the end of an input string using the join() function inputString_1 = ''.join((inputString_1,'t')) # printing the resultant string print("Adding 't' at the end of an inputString_1:\n", inputString_1) # input string 2 inputString_2 = "tutorialsoint" # joining character 'p' at a specific position i.e, at index 9 inputString_2 = ''.join((inputString_2[:9],'p',inputString_2[9:])) print("Adding 'p' at 9th index of an inputString_2:\n", inputString_2) inputString_3 = "utorialspoint" # joining character 't' at the starting of an input string inputString_3 = ''.join(("t",inputString_3)) print("Adding 't' at the starting of an inputString_3:\n", inputString_3)

Output

Adding 't' at the end of an inputString_1:
 tutorialspoint
Adding 'p' at 9th index of an inputString_2:
 tutorialspoint
Adding 't' at the starting of an inputString_3:
 tutorialspoint

Method 3: Using the format() function

Python's format() function can be used to add characters to a specific position in a string. Python also employs it for string formatting.

Adding Character at the end of a String

Algorithm 

Following are the Algorithms/steps to be followed to perform the desired task. ?

  • Use the format() function to add a character at the end of an input string by formatting it at the last index.

  • Print the resultant string.

  • In the same way, add the characters at the specific position and starting of a string by formatting it at the required indexes using the format() function.

Example

The following program adds a character at the end of an input string using the format() function ?

Open Compiler
# input string 1 inputString_1 = "tutorialspoin" # adding a character at the end of an input string using the format() function inputString_1 = "tutorialspoin{}".format('t') print("Adding 't' at the end of an inputString_1:\n", inputString_1)

Output

Adding 't' at the end of an input string:
 tutorialspoint

Adding Character at a Specific Position

Example

The following program adds a character at the end of an input string using the format() function ?

Open Compiler
# input string 2 inputString_2 = "tutorialsoint" # adding character 'p' at a specific position i.e, at index 9 # by formatting it using format() function inputString_2= "tutorials{}oint".format('p') # printing the resultant string print("Adding 'p' at 9th index of an inputString_2:\n", inputString_2)

Output

Adding 'p' at 9th index of an inputString_2:
 tutorialspoint

Adding Character at Starting Position of a String

Example

The following program adds a character at the end of an input string using the format() function ?

Open Compiler
# input string 3 inputString_3 = "utorialspoint" # adding character 't' at the starting of an inputString_3 # by formatting it at index 0 inputString_3 = "{}utorialspoint".format('t') # printing the resultant string print("Adding 't' at the starting of an inputString_3:\n", inputString_3)

Output

Adding 't' at the starting of an inputString_3:
 tutorialspoint

Conclusion

Through the use of the join(), + operator, and format() functions, we learned how to add a character to a certain position in a string using three different approaches in this article. We may join any list, tuple, or other data type to a string with a delimiter using the join() method.

Updated on: 2023-08-25T00:51:31+05:30

65K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements