
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Add a character to a specific position in a 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 −
# 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 −
# 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 −
# 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 −
# 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 −
# 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 −
# 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 −
# 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.
- Related Articles
- Python Program to Replace the Spaces of a String with a Specific Character
- How to delete a character from a string using python?
- How to add space before and after specific character using regex in Python?
- Insert a character at nth position in string in JavaScript
- How to add elements to AbstractSequentialList class at a specific position in Java?
- Take off last character if a specific one exists in a string?
- Python program to change character of a string using given index
- How to add a specific character to any empty space in MySQL table values?
- Java Program to Replace the Spaces of a String with a Specific Character
- Set characters at a specific position within the string in Arduino
- How to find a unique character in a string using java?
- How to add a character to the beginning of every word in a string in JavaScript?
- Golang program to replace the spaces of string with a specific character
- First Unique Character in a String in Python
- Python Pandas - Insert a new index value at a specific position
