capitalize()
This function is used to capitalize the first letter of the string.
Example:
str="hello world" print(str.capitalize())
Output:
Hello world
count(str, beg, end)
This function is used to count the number of times str occurs in a string. We can specify beg as 0 and end as the length of the message to search the entire string or use any other value to just search a part of the string.
Example:
str="we" mess="weareweare" print(mess.count(str,0,len(mess)))
Output:
2
endswith(suffix, beg, end)
This function is used to check if the string ends with a suffix; it returns True if so and False otherwise. We can either set beg=0 and end equal to the length of the message to search the entire string or use any other value to search a part of it.
Example:
str="hello" mess="helloworldhello" print(mess.endswith("hello",0,len(mess)))
Output:
True
startswith(prefix, beg, end)
This function is used to check if the string starts with a prefix; it returns True if so and False otherwise. We can either set beg=0 and end equal to the length of the message to search the entire string or use any other value to search a part of it.
Example:
str="hello" mess="helloworldhello" print(mess.startswith("hello",0,len(mess)))
Output:
True
lower()
This function is used to convert all characters in the string into lowercase.
Example:
str="WELCOME" print(str.lower())
Output:
Welcome
upper()
This function is used to convert all characters in the string into uppercase.
Example:
str="welcome" print(str.upper())
Output:
WELCOME
strip()
This function is used to remove all leading and trailing whitespace in string.
Example:
str=" Happy New Year 2021 " print(str.strip())
Output:
Happy New Year 2021
lstrip()
This function is used to remove all leading whitespace in the string.
Example:
str=" Apple is Red or Green" print(str.lstrip())
Output:
Apple is Red or Green
strip()
This function is used to remove all trailing whitespace in the string.
Example:
str="Apple is Red or Green " print(str.rstrip())
Output:
Apple is Red or Green
replace(old, new [, max])
This function is used to replace all or max (if given) occurrences of old in a string with new.
Example:
str="Python programming lang" print(str.replace("lang","language"))
Output:
Python programming language
title()
This function is used as a return string in the title case.
Example:
str="programming language world" print(str.title())
Output:
Programming Language World
len()
This function is used to return the length of the string.
Example:
str="programming language world" print(len(str))
Output:
26
Leave a comment