Python has a set of built-in methods that we can use on a given strings.
The upper() method returns the string in upper case:
mystr="Welcome to Neoogy"
print(mystr.upper())
print(mystr.upper())
Output will be
WELCOME TO NEOOGY
The lower() method returns the string in lower case:
mystr="Welcome to Neoogy"
print(mystr.lower())
print(mystr.lower())
Output will be
welcome to neoogy
The capitalize() method returns the first character to upper case:
mystr="welcome to neoogy"
print(mystr.capitalize())
print(mystr.capitalize())
Output will be
Welcome to neoogy
The swapcase() method returns the string in reverse case:
mystr="Welcome to Neoogy"
print(mystr.swapcase())
print(mystr.swapcase())
Output will be
wELCOME TO nEOOGY
The title() method returns the first character of each word to upper case:
mystr="welcome to neoogy"
print(mystr.title())
print(mystr.title())
Output will be
Welcome To Neoogy
The isalpha() method returns True if all characters in the string are in the alphabet:
mystr="Neoogy"
print(mystr.isalpha())
print(mystr.isalpha())
Output will be
True
The isalnum() method returns True if all characters in the string are alphanumeric:
mystr="Neoogy1"
print(mystr.isalnum())
print(mystr.isalnum())
Output will be
True
The isdigit() method returns True if all characters in the string are digits:
mystr="1234567"
print(mystr.isdigit())
print(mystr.isdigit())
Output will be
True
The replace() method returns a string where a specified value is replaced with a specified value:
mystr="Google"
print(mystr.replace("G","N")) #Here G is replace with N
print(mystr.replace("G","N")) #Here G is replace with N
Output will be
Noogle
The index() method returns the index value (First occurrence) of the chatacter from a string:
mystr="Neoogy"
print(mystr.index("o"))
print(mystr.index("o"))
Output will be
2
The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove):
mystr=" Neoogy "
print(mystr.strip())
print(mystr.strip())
Output will be
Neoogy
OR
mystr="W.Neoogy,,;,"
print(mystr.strip("W.;,"))
print(mystr.strip("W.;,"))
Output will be
Neoogy
The split() method splits a string into a list.(By default it split a string in whitespace)
mystr="Welcome to Neoogy"
print(mystr.split())
print(mystr.split())
Output will be
['Welcome', 'to', 'Neoogy']
OR
mystr="Welcome to, Neoogy"
print(mystr.split(",")) #Split the string, using comma
print(mystr.split(",")) #Split the string, using comma
Output will be
['Welcome to', ' Neoogy']
« Previous Next »
0 Comments