Rion Louji
4 min readJun 3, 2021

String Methods:

There are 44 different types of string methods which are listed below with an example.

a=str(input("Enter a string: ")) #Getting Input from the userprint("String Method-1 : capitalize() -- Converts the first character to upper case","\n", a.capitalize())print("\n\n")print("String Method-2 : casefold() -- Converts string into lower case\n", a.casefold())print("\n\n")print("String Method-3 : center() -- Returns a centered string\n",a.center(50,'-'))print("\n\n")b=str(input("Enter string: "))#Getting inputprint("String Method-4 : count() -- Returns the number of times a specified value occurs in a string\n",b.count("and"))print("\n\n")print("String Method-5 : encode() --Returns an encoded version of the string\n",b.encode())print("\n\n")print("String Method-6 : endswith() --Returns true if the string ends with the specified value\n",b.endswith("."))print("\n\n")c="H\te\tl\tl\to"print("String Method-7 : expandtabs() --Sets the tab size of the string\n",c.expandtabs(10))print("\n\n")print("String Method-8 : find() --Searches the string for a specified value and returns the position of where it was found\n",b.find("and"))print("\n\n")d="Value of 1 dollar in INR is {price:.2f}"print("String Method-9 : format() --Formats specified values in a string\n",d.format(price=73))print("\n\n")e={'x':10,'y':1}print("String Method-10 : format_map() --Formats specified values in a string\n",'{x} {y}'.format(**e))print("\n\n")g="Python is a crash course and it is a awesome language"
print("String Method-11 : index() --Searches the string for a specified value and returns the position of where it was found\n",c,":",g.index("and"))
print("\n\n")f="hello_world"print("String Method-12 : isalnum() --Returns True if all characters in the string are alphanumeric\n",f,":",f.isalnum())print("\n\n")print("String Method-13 : isalnum() -- Returns True if all characters in the string are alphanumeric\n",f,":",f.isalnum())print("\n\n")print("String Method-14 : isaplha() --Returns True if all characters in the string are in the alphabet\n",f,":",f.isalpha())print("\n\n")h="0033"print("String Method-15 : isdecimal() --Returns True if all characters in the string are decimals\n",h,":",h.isdecimal())print("\n\n")print("String Method-16 : isdigit() --Returns True if all characters in the string are digits\n",h,":",h.isdigit())print("\n\n")i="hello world"print("String Method-17 : isidentifier() --Returns True if the string is an identifier\n",f,":",f.isidentifier(),"\n",i,":",i.isidentifier())print("\n\n")print("String Module-18 : islower() --Returns True if all characters in the string are lower case\n",i,":",i.islower())print("\n\n")print("String Method-19 : isnumeric() --Returns True if all characters in the string are numeric\n",h,":",h.isnumeric(),"\n",i,":",i.isnumeric())print("\n\n")print("String Method-20 : isprintable() --Returns True if all characters in the string are printable\n",i,":",i.isprintable(),"\n",c,":",c.isprintable())print("\n\n")j=" "print("String Module-21 : isspace() --Returns True if all characters in the string are whitespaces\n",j,":",j.isspace(),"\n",i,":",i.isspace())print("\n\n")k="Hello"print("String Method-22 : istitle() --Returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise False")print(i,":",i.istitle())print(k,":",k.istitle())print("\n\n")l="HELLO"print("String Method-23 : isupper --Returns True if all characters in the string are upper case\n",k,":",k.isupper(),"\n",l,":",l.isupper())print("\n\n")m={"country": "INDIA", "currency": "Rupees"}print("String Method-24 : join() --Joins the elements of an iterable to the end of the string\n", l.join(m))print("\n\n")print("String Method-25 : ljust() --Returns a left justified version of the string\n",l.ljust(10),"WORLD")print("\n\n")print("String Method-26 : lower() --Converts a string into lower case\n",l,":",l.lower())print("\n\n")n="....,,,aasswww....Hello"print("String Method-27 : lstrip() --Returns a left trim version of the string\n",n,":",n.lstrip(".,asw"))print("\n\n")print("String Method-28 : maketrans() --Returns a translation table to be used in translations")print(l,":",l.maketrans("H","R")) #Both the parameters must have same lengthprint(l,":",l.maketrans("HEL","DYN","L")) #third parameter removes the mentioned values from the stringprint("\n\n")o="Python is Portable Language"print("String Method-29 : partition() --Returns a tuple where the string is parted into three parts\n",o,":",o.partition("easy"))print("\n\n")print("String Method-30 : replace() --Returns a string where a specified value is replaced with a specified value")print(o.replace("Portable","easy"))print("\n\n")print("String Method-31 : rfind() --Searches the string for a specified value and returns the last position of where it was found")print(o.rfind("Language"))print("\n\n")print("String Method-32 : rindex() --Searches the string for a specified value and returns the last position of where it was found")print(o.rindex("Portable"))print("\n\n")print("String Method-33 : rjust() --Returns a right justified version of the string")print(l.rjust(10),"WORLD")print("\n\n")print("String Method-34 : rpartition() --Returns a tuple where the string is parted into three parts")print(o,":",o.rpartition("easy"))print("\n\n")print("String Method-35 : rsplit() --Splits the string at the specified separator, and returns a list")p="dollar,rupees,euro"print(p.rsplit(","))print("\n\n")print("String Method-36 : rstrip() --Returns a right trim version of the string")print(o,":",o.rstrip("Language"))print("\n\n")print("String Method-37 : splitlines() --Splits the string at line breaks and returns a list")q="Python\n Data Structures"print(q,":",q.splitlines(True))print("\n\n")print("String Method-38 : startswith() --Returns true if the string starts with the specified value")print(o,": Startswith Python",":",o.startswith("Python"))print("\n\n")print("String Method-39 : strip() --Returns a trimmed version of the string") #Removes Unwanted spacesprint(n,":",n.strip("...,,,asw"))print("\n\n")print("String Method-40 : swapcase() --Swaps cases, lower case becomes upper case and vice versa")print(o,":",o.swapcase())print("\n\n")print("String Method-41 : title() --Converts the first character of each word to upper case")r="apple is a fruit"print(r,":",r.title())print("\n\n")print("String Method-42 : translate() --Returns a translated string") #use dictionary with ascii codesmdict={72 : 67}print(k,":",k.translate(mdict))print("\n\n")print("String Method-43 : upper() --Converts a string into upper case")print(r,":",r.upper())print("\n\n")print("String Method-44 : zfill() --Fills the string with a specified number of 0 values at the beginning")
x="199"
print(x.zfill(20))

by,

S.Rion Louji