Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. Python strings are immutable, so these methods create new strings rather than modifying the original.
Methods to Add Characters to Strings
The following are the various methods to accomplish this task ?
Using the '+' operator
Using join() function
Using format() function
Method 1: Using the '+' Operator
The '+' operator concatenates strings by combining them at specified positions. This is the most straightforward method for adding characters.
Adding Character at the End
The following program adds a character at the end of a string using the '+' operator ?
# input string
input_string = "tutorialspoin"
# adding a character at the end using the + operator
result = input_string + 't'
# printing the resultant string
print("Adding 't' at the end:", result)
Adding 't' at the end: tutorialspoint
Adding Character at a Specific Position
Use string slicing to insert a character at any position within the string ?
# input string
input_string = "tutorialsoint"
# adding character 'p' at index 9 using slicing
result = input_string[:9] + 'p' + input_string[9:]
# printing the resultant string
print("Adding 'p' at index 9:", result)
Adding 'p' at index 9: tutorialspoint
Adding Character at the Beginning
The following program adds a character at the start of a string ?
# input string
input_string = "utorialspoint"
# adding character 't' at the beginning
result = 't' + input_string
# printing the resultant string
print("Adding 't' at the beginning:", result)
Adding 't' at the beginning: tutorialspoint
Method 2: Using the join() Function
The join() function concatenates sequence elements with a specified separator. It's efficient for combining multiple parts of a string.
Example
The following program demonstrates adding characters at different positions using join() ?
# Adding at the end
input_string_1 = "tutorialspoin"
result_1 = ''.join([input_string_1, 't'])
print("Adding 't' at the end:", result_1)
# Adding at specific position (index 9)
input_string_2 = "tutorialsoint"
result_2 = ''.join([input_string_2[:9], 'p', input_string_2[9:]])
print("Adding 'p' at index 9:", result_2)
# Adding at the beginning
input_string_3 = "utorialspoint"
result_3 = ''.join(['t', input_string_3])
print("Adding 't' at the beginning:", result_3)
Adding 't' at the end: tutorialspoint Adding 'p' at index 9: tutorialspoint Adding 't' at the beginning: tutorialspoint
Method 3: Using the format() Function
Python's format() function allows inserting values into placeholders within strings. This method is useful for template-based string construction.
Adding Character at Different Positions
The following program demonstrates using format() to add characters ?
# Adding at the end
result_1 = "tutorialspoin{}".format('t')
print("Adding 't' at the end:", result_1)
# Adding at specific position
result_2 = "tutorials{}oint".format('p')
print("Adding 'p' at index 9:", result_2)
# Adding at the beginning
result_3 = "{}utorialspoint".format('t')
print("Adding 't' at the beginning:", result_3)
Adding 't' at the end: tutorialspoint Adding 'p' at index 9: tutorialspoint Adding 't' at the beginning: tutorialspoint
Comparison of Methods
| Method | Best For | Readability | Performance |
|---|---|---|---|
| '+' Operator | Simple concatenation | High | Good for few operations |
| join() | Multiple string parts | Medium | Efficient for many operations |
| format() | Template-based insertion | High | Good with formatting needs |
Conclusion
The '+' operator is most intuitive for simple character insertion, while join() is efficient for multiple concatenations. Use format() when you need template-based string construction with placeholders.
