@ilyasqasim
Python
Modify
Strings
resources: www.w3schools.com
01
Python Modify Strings
Python has a set of built-in methods that you can use on
strings.
Upper Case
The upper() method returns the string in upper case:
a = "Python, World!"
print(a.upper())
Result:
PYTHON, WORLD!
Lower Case
The lower() method returns the string in lower case:
a = "Python, World!"
print(a.lower())
Result:
python, world!
@ilyasqasim resources: www.w3schools.com
02
Remove Whitespace
Whitespace is the space before and/or after the actual text, and
very often you want to remove this space.
2
The strip() method removes any whitespace from the beginning
or the end:
a = " Python, World! “
print(a.strip())
Result:
Python, World!
Replace String
The replace() method replaces a string with another string:
a = " Python, World! “
print(a.replace(“o”, “a”))
Result:
Pythan, Warld!
@ilyasqasim resources: www.w3schools.com
03
Split String
The split() method returns a list where the text between the
specified separator becomes the list items.
The split() method splits the string into substrings if it finds
instances of the separator:
a = "Python, World!" The split() method splits a string into a
print(a.split(“,”)) list:
Split a string into a list where each word is a
list item
Result:
You can specify the separator, default
['Python', ' World!'] separator is any whitespace.
"CURIOUS ABOUT PYTHON
STRINGS?
ASK IN THE COMMENTS
AND I'LL SHARE THE
COMPREHENSIVE LIST OF
BUILT-IN METHODS WITH
EXPLANATIONS!"
@ilyasqasim resources: www.w3schools.com
J
O
LIKE COMMENT REPOST SHARE
I Learn to Code
@ilyasqasim
resources:
www.w3schools.com