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)
1. Accessing the character of a string
# For viewing items in a string, type. string = "HELLO WORLD!" data_in_string = string print(data_in_string)
In the given string “HELLO WORLD!”, the character ‘L’ is placed at the 2nd index therefore string returns ‘L’.
2. Length of String
We can find the length of the string using len().
string = "HELLO WORLD!" print(len(string))
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"))
4. Mutate a String in Python by Slicing
To get a set of letters, type[ # : #].
string = "Hello World!" # first char print(string) # 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])
5. Mutate a String in Python by Splitting
string = "Hello World!" print(string.split(' '))
6. Starts with or Ends with
string = "Hello World!" print(string.startswith("H")) print(string.endswith("d")) print(string.endswith("!")) print(string.endswith("o"))
7. Mutate a String in Python by Replacing
string = "Hello World!" print(string.replace("Hello", "Bye"))
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())
9. Mutate a String in Python by Reversing
str = "Hello world!" print(' '.join(reversed(str)))
! 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)
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!