Skip to content

Python Modify Strings

In the python, strings are immutable. This means that once a string is created, we cannot change it. We can only create a new string. But we can modify the existing string by reassigning a variable to another string. The new string can be a substring of the original string or a concatenation of two or more strings. Letโ€™s look at some examples.

Example 1: Modify a String

strings.py
# define a string
string = "Hello World"
string = string[:6] + "Python"
print(string)
strings.py
# define a string
string = "Hello World"
string = string[:6] + "Python"
print(string)

Output:

command
C:\Users\Your Name> python strings.py
Hello Python
command
C:\Users\Your Name> python strings.py
Hello Python

In the above example, we have modified the string by assigning a new string to the variable stringstring. The new string is a concatenation of the first six characters of the original string and the string "Python""Python".

Convert String to List

In Python, a string canโ€™t be updated but we can convert a string to a list and modify the list. After making the required changes, we can convert the list back to a string. We can use the list()list() function to convert a string to a list, and the str()str() function to convert a list back to a string.

strings.py
# define a string
string = "Hello World"
# convert the string to a list
string = list(string)
# change the 7th character of the list
string[6] = 'P'
# convert the list back to a string
string = ''.join(string)
print(string)
strings.py
# define a string
string = "Hello World"
# convert the string to a list
string = list(string)
# change the 7th character of the list
string[6] = 'P'
# convert the list back to a string
string = ''.join(string)
print(string)

Output:

command
C:\Users\Your Name> python strings.py
Hello Porld
command
C:\Users\Your Name> python strings.py
Hello Porld

In the above example, we have converted the string to a list, changed the 7th character of the list, and converted the list back to a string.

append() and insert() Methods

We can also use the append()append() and insert()insert() methods to modify strings in Python. The append()append() method adds a single character to the end of the string, and the insert()insert() method adds a single character at the specified index.

strings.py
# define a string
string = "Hello World"
# convert the string to a list
string = list(string)
# append the letter 'P' to the list
string.append('P')
# insert the letter 'y' at index 6
string.insert(6, 'y')
# convert the list back to a string
string = ''.join(string)
print(string)
strings.py
# define a string
string = "Hello World"
# convert the string to a list
string = list(string)
# append the letter 'P' to the list
string.append('P')
# insert the letter 'y' at index 6
string.insert(6, 'y')
# convert the list back to a string
string = ''.join(string)
print(string)

Output:

command
C:\Users\Your Name> python strings.py
Hello PyWorld
command
C:\Users\Your Name> python strings.py
Hello PyWorld

In the above example, we have converted the string to a list, appended the letter 'P''P' to the list, inserted the letter 'y''y' at index 6, and converted the list back to a string.

Delete a String

We can delete a string by using the deldel keyword. The deldel keyword can delete a single character, a range of characters, or an entire string.

strings.py
# define a string
string = "Hello World"
# delete the letter 'H'
del string[0]
print(string)
# delete the letters 'el' from index 1 to index 3
del string[1:4]
print(string)
# delete the entire string
del string
print(string)
strings.py
# define a string
string = "Hello World"
# delete the letter 'H'
del string[0]
print(string)
# delete the letters 'el' from index 1 to index 3
del string[1:4]
print(string)
# delete the entire string
del string
print(string)

Output:

command
C:\Users\Your Name> python strings.py
ello World
eo World
Traceback (most recent call last):
  File "strings.py", line 10, in <module>
    print(string)
NameError: name 'string' is not defined
command
C:\Users\Your Name> python strings.py
ello World
eo World
Traceback (most recent call last):
  File "strings.py", line 10, in <module>
    print(string)
NameError: name 'string' is not defined

In the above example, we have deleted the letter 'H''H' from the string, deleted the letters 'el''el' from index 1 to index 3, and deleted the entire string.

Conclusion

In this tutorial, we have learned how to modify strings in Python. We have also learned that strings are immutable in Python. This means that once a string is created, we cannot change it. We can only create a new string. We can modify the existing string by reassigning a variable to another string. The new string can be a substring of the original string or a concatenation of two or more strings. We can also convert a string to a list and modify the list. After making the required changes, we can convert the list back to a string. We can also use the append()append() and insert()insert() methods to modify strings in Python. The append()append() method adds a single character to the end of the string, and the insert()insert() method adds a single character at the specified index. We can delete a string by using the deldel keyword. The deldel keyword can delete a single character, a range of characters, or an entire string. For more tutorials on Python, check out our Python Central Hub.

Was this page helpful?

Let us know how we did