How to Mutate a String in Python

How to Mutate a String in Python | A string is defined as a collection of characters. A character is whatever you can enter in a single stroke on the keyboard, like a letter or an integer. Spaces are also allowed in strings.

Python has mutable and immutable data types. In layman’s terms, mutable means ‘changeable,’ while immutable means ‘unchangeable’. 

Any modifications to the values are mirrored in both variables when you assign a variable to some other variable of the mutable datatype. Everything enclosed by quote marks (” ” or”) is understood by Python as a string.

However, strings in python are immutable. But they can still be manipulated by using some built-in methods in the language. We will see a program that will show all the possible ways for the mutation of a string in Python.

# A simple Hello World program in Python
string = "Hello World"
print(string)

Output:-

Hello World

1. Accessing the character of a string

# For viewing items in a string, type[].
string = "HELLO WORLD!"
data_in_string = string[2]
print(data_in_string)

Output:-

L

In the given string “HELLO WORLD!”, the character ‘L’ is placed at the 2nd index therefore string[2] returns ‘L’.

2. Length of String

We can find the length of the string using len().

string = "HELLO WORLD!"
print(len(string))

Output:-

12

3. Finding a substring or character in a string

The dot operator is used to retrieve list items by built-in methods.

string = "hello world"
# to find "e"
print(string.find("e"))
# to count
print(string.count('o'))
# to find "world"
print(string.index("world"))

Output:-

1
2
6

4. Mutate a String in Python by Slicing

To get a set of letters, type[ # : #].

string = "Hello World!"

# first char
print(string[0])

# for the first three char
print(string[:3])

# for the last three char
print(string[-3:])

# everything but the first three
print(string[3:])

# everything but the last three
print(string[:-3])

Output:-

H
Hel
ld!
lo World!
Hello Wor

5. Mutate a String in Python by Splitting

string = "Hello World!"
print(string.split(' '))

Output:-

[‘Hello’, ‘World!’]

6. Starts with or Ends with

string = "Hello World!"
print(string.startswith("H"))
print(string.endswith("d"))
print(string.endswith("!"))
print(string.endswith("o"))

Output:-

True
False
True
False

7. Mutate a String in Python by Replacing

string = "Hello World!"
print(string.replace("Hello", "Bye"))

Output:-

Bye World!

8. Mutate a String in Python by changing Upper and Lower Case Strings

str = "HelLo wOrld!"
print(str.upper())
print(str.lower())
print(str.title())
print(str.capitalize())
print(str.swapcase())

Output:-

HELLO WORLD!
hello world!
Hello World!
Hello world!
hELlO WoRLD!

9. Mutate a String in Python by Reversing

str = "Hello world!"
print(' '.join(reversed(str)))

Output:-

! d l r o w o l l e H

10. Mutate a String in Python by Concatenation

In Python, the “+” operator is used to combine multiple strings.

a = "Hello " + "world"
print(a)
b = "Hello " + "world" + "!!"
print(b)

Output:-

Hello world
Hello world!!

Thus we saw various methods for the mutation of strings. We provided you with the simplest examples to help you understand the concept better. Also see:- Find Shortest Word in List Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *