Python Strings: Replace, Join, Split, Reverse
โก Smart Summary
Python strings are immutable sequences of characters with many built-in methods for text processing. The examples below demonstrate slicing, string operators, replace(), upper() and lower(), join(), reversing, and split() using runnable code.

In Python, everything is an object, and strings are objects too. A Python string can be created simply by enclosing characters in double quotes.
For example:
var = โHello World!โ
Accessing Values in Strings
Python does not support a character type; these are treated as strings of length one, also considered as a substring.
We use square brackets for slicing along with the index or indices to obtain a substring.
var1 = "Guru99!"
var2 = "Software Testing"
print ("var1[0]:",var1[0])
print ("var2[1:5]:",var2[1:5])
Output:
var1[0]: G var2[1:5]: oftw
Various String Operators
There are various string operators that can be used in different ways, such as concatenating a different string.
Suppose if a=guru and b=99, then a+b= โguru99โ. Similarly, if you are using a*2, it will give โGuruGuruโ. Likewise, you can use other operators in a string.
| Operator | Description | Example |
|---|---|---|
| [] | Slice – it gives the letter from the given index | a[1] will give โuโ from the word Guru as such ( 0=G, 1=u, 2=r and 3=u)
x="Guru" print (x[1]) |
| [ : ] | Range slice – it gives the characters from the given range | x [1:3] it will give โurโ from the word Guru. Remember it will not consider 0 which is G, it will consider word after that is ur.
x="Guru" print (x[1:3]) |
| in | Membership – returns true if a letter exists in the given string | u is present in word Guru and hence it will give 1 (True)
x="Guru"
print ("u" in x)
|
| not in | Membership – returns true if a letter does not exist in the given string | l is not present in word Guru and hence it will give 1
x="Guru"
print ("l" not in x)
|
| r/R | Raw string suppresses actual meaning of escape characters. | Print rโ\nโ prints \n and print Rโ/nโ prints \n |
| % โ Used for string format | %r โ It inserts the canonical string representation of the object (i.e., repr(o)) %s – It inserts the presentation string representation of the object (i.e., str(o)) %d – it will format a number for display | The output of this code will be โguru 99โ.
name = 'guru'
number = 99
print ('%s %d' % (name,number))
|
| + | It concatenates 2 strings | It concatenates strings and gives the result
x="Guru" y="99" print (x+y) |
| * | Repeat | It prints the character twice.
x="Guru" y="99" print (x*2) |
Some more examples
You can update a Python string by re-assigning a variable to another string. The new value can be related to a previous value, or to a completely different string altogether.
x = "Hello World!" print(x[:6]) print(x[0:6] + "Guru99")
Output:
Hello Hello Guru99
Note: – Slice [:6] or [0:6] has the same effect.
Python String replace() Method
The replace() method returns a copy of the string in which the values of the old string have been replaced with the new value.
oldstring = 'I like Guru99'
newstring = oldstring.replace('like', 'love')
print(newstring)
Output:
I love Guru99
Changing upper and lower case strings
In Python, you can even change the string to upper case or lower case.
string="python at guru99" print(string.upper())
Output:
PYTHON AT GURU99
Likewise, you can also use other functions such as capitalize().
string="python at guru99" print(string.capitalize())
Output:
Python at guru99
You can also convert your string to lower case.
string="PYTHON AT GURU99" print(string.lower())
Output:
python at guru99
Using โjoinโ function for the string
The join() function is a more flexible way of concatenating a string. With the join() function, you can add any character into the string.
For example, if you want to add a colon (:) after every character in the string โPythonโ, you can use the following code.
print(":".join("Python"))
Output:
P:y:t:h:o:n
Reversing String
By using the reversed() function, you can reverse the string. For example, if we have the string โ12345โ and then apply the code for the reversed() function as shown below.
string="12345"
print(''.join(reversed(string)))
Output:
54321
Split Strings
Split strings is another function that can be applied in Python. Let us see it for the string โguru99 career guru99โ. First, here we will split the string by using the command word.split and get the result.
word="guru99 career guru99"
print(word.split(' '))
Output:
['guru99', 'career', 'guru99']
To understand this better, we will see one more example of split. Instead of a space (โ โ), we will replace it with (โrโ), and it will split the string wherever โrโ is mentioned in the string.
word="guru99 career guru99"
print(word.split('r'))
Output:
['gu', 'u99 ca', 'ee', ' gu', 'u99']
Important Note: In Python, strings are immutable.
Consider the following code:
x = "Guru99"
x.replace("Guru99","Python")
print(x)
Output:
Guru99
It will still return Guru99. This is because x.replace(โGuru99โ,โPythonโ) returns a copy of X with replacements made.
You will need to use the following code to observe the changes.
x = "Guru99"
x = x.replace("Guru99","Python")
print(x)
Output:
Python
The above codes are Python 3 examples. If you want to run them in Python 2, please consider the following code.
Python 2 Example
#Accessing Values in Strings
var1 = "Guru99!"
var2 = "Software Testing"
print "var1[0]:",var1[0]
print "var2[1:5]:",var2[1:5]
#Some more examples
x = "Hello World!"
print x[:6]
print x[0:6] + "Guru99"
#Python String replace() Method
oldstring = 'I like Guru99'
newstring = oldstring.replace('like', 'love')
print newstring
#Changing upper and lower case strings
string="python at guru99"
print string.upper()
string="python at guru99"
print string.capitalize()
string="PYTHON AT GURU99"
print string.lower()
#Using "join" function for the string
print":".join("Python")
#Reversing String
string="12345"
print''.join(reversed(string))
#Split Strings
word="guru99 career guru99"
print word.split(' ')
word="guru99 career guru99"
print word.split('r')
x = "Guru99"
x.replace("Guru99","Python")
print x
x = "Guru99"
x = x.replace("Guru99","Python")
print x
Output:
var1[0]: G var2[1:5]: oftw Hello Hello Guru99 I love Guru99 PYTHON AT GURU99 Python at guru99 python at guru99 P:y:t:h:o:n 54321 ['guru99', 'career', 'guru99'] ['gu', 'u99 ca', 'ee', ' gu', 'u99'] Guru99 Python
Python has introduced a .format() function which does away with using the cumbersome %d and so on for string formatting.
ยป Learn more about Python String split()
