IT & IoT Security | Cloud | It's all about the life itself

Nothing in life is as important as you think it is, while you are thinking about it.

Python – useful string methods

Hi all, here is a brief list of useful string methods.

capitalize(). Makes the first letter of the string uppercase and all the rest lowercase, and returns the result.
lower(). Returns a version of the string with all uppercase letters changed to lowercase.
upper(). Returns a version of the string with all lowercase letters changed to uppercase.
title(). Returns a version of the string with each word (e.g., letter following a space) capitalized.
strip(). Returns a version of the string with any whitespaces (spaces, line breaks, etc.) at the beginning and end of the string removed. rstrip() and lstrip() apply this strip() method only to the right or left sides of the string.
replace(old, new). Replace all occurrences of the substring old with the substring new.
rfind(findString). Just like find(), but returns the last index of findString instead of the first.
join(list). Creates a string where each item in the list is followed by the string, and returns the result.

endswith(suffix). Returns True if the string ends with suffix, False if not.
startswith(prefix). Returns True if the string starts with prefix, False if not.
isalnum(). Returns True if the string is all letters and numbers, False if not.
isalpha(). Returns True if the string is all letters, False if not.
isdecimal(). Returns True if the string represents an integer or decimal number, False if not.
isdigit(). Returns true if the string is all numbers (e.g., represents an integer), False if not. isnumeric() is similar, but supports fraction and other characters as well (which are rarely used).
islower(). Returns True if the string contains no uppercase letters, False if not.
isupper(). Returns True if the string contains no lowercase letters, False if not.
istitle(). Returns True if the string is in title case, meaning each word is capitalized, and False if not.

Here is a sample:

str1 = "this is MY test string!   "
print("Original state: "+"\t\t\t"+str1)
print("with Capitalize method: "+"\t"+str1.capitalize())
print("with Lower method: "+"\t\t\t"+str1.lower())
print("with Upper method: "+"\t\t\t"+str1.upper())
print("with Title method: "+"\t\t\t"+str1.title())
print("with Capitalize method: "+"\t"+str1.capitalize())
print("with Strip method: "+"\t\t\t"+str1.strip())
print("with Replace method: "+"\t\t"+str1.replace("MY","YOUR"))

list1 = str1.split()
print(list1)
new_str1 = "%".join(list1)
print(new_str1.split("s"))

The output will be;
Original state: 			this is MY test string!   
with Capitalize method: 	This is my test string!   
with Lower method: 			this is my test string!   
with Upper method: 			THIS IS MY TEST STRING!   
with Title method: 			This Is My Test String!   
with Capitalize method: 	This is my test string!   
with Strip method: 			this is MY test string!
with Replace method: 		this is YOUR test string!   
['this', 'is', 'MY', 'test', 'string!']
['thi', '%i', '%MY%te', 't%', 'tring!']