1- capitalize()
Capitalizes the first letter of a string.
string = 'what is your name?'
capitalized = string.capitalize()
print(capitalized) # 'What is your name?'
2- lower()
Changes all characters in a string to lowercase.
string = 'What Is Your Name?'
lowered = string.lower()
print(lowered) # 'what is your name?'
3- upper()
Changes all characters in a string to uppercase.
string = 'What Is Your Name?'
uppered = string.upper()
print(uppered) # 'WHAT IS YOUR NAME?'
4- swapcase()
Interchange the cases of all characters in a string.
string = 'What Is Your Name?'
swapcased = string.swapcase()
print(swapcased) # 'WHAT IS YOUR NAME?'
5- title()
Capitalizes the first letter of each word in a string.
string = 'what is your name?'
titled = string.title()
print(titled) # 'What Is Your Name?'
6- strip()
Removes whitespace from the start and end of a string.
string = ' what is your name? '
stripped = string.strip()
print(stripped) # 'what is your name?'
7- count()
Gives the number of occurrences of a substring in a string.
string = 'what is your name?'
counted = string.count('a')
print(counted) # 2
8- find()
Gives the index of the first occurrence of a substring in a string, or -1 if it is not present.
string = 'what is your name?'
found = string.find('is')
print(found) # 5
9- index()
Gives the index of the first occurrence of a substring in a string, or raises a ValueError if it is not present.
string = 'what is your name?'
indexed = string.index('your')
print(indexed) # 9
10- replace()
Interchange all occurrences of a substring in a string with another substring.
string = 'what is your name?'
replaced = string.replace('your', 'my')
print(replaced) # 'what is my name?'
11- format()
Formats a string using placeholders for variables or values.
string = 'what is your name?'
replaced = string.replace('your', 'my')
print(replaced) # 'what is my name?'
12- split()
Splits a string into a list of substrings using a specified delimiter.
formatted = 'My name is {name} and I am {age} years old'.format(name='NK', age=23)
print(formatted) # 'My name is Jane and I am 30 years old'
13- join()
Joins a list of strings into a single string using a specified character or symbols.
string = 'what is your name?'
splitted = string.split()
print(splitted) # ['what', 'is', 'your', 'name?']
14- startswith()
Gives True if a string starts with a specified substring, otherwise False.
joined = '-'.join(['what', 'is', 'your', 'name?'])
print(joined) # 'what-is-your-name?'
15- endswith()
Gives True if a string ends with a specified substring, otherwise False.
string = 'what is your name?'
startswith = string.startswith('what')
print(startswith) # True
Top comments (0)