String Methods in Python with Syntax and Examples

What is String in Python?

The string is a collection of characters enclosed within single quotes, double quotes, and triple quotes. We can also say that a Python string is a collection of Unicode characters. In Python, a string can be created by enclosing the character or collection of characters in the quotes. A string is the most popular data type in Python.


String Methods in Python with Syntax and Examples


Syntax

myString = "This is a string!"

Python String Methods

Python has a collection of built-in methods that you can use for strings. All string methods return new values and they don't change the original string. Now let's see all the Python String Methods with Syntax and Examples


1.  capitalize()

This method will convert the first character of a string to upper case.

myString = "hey, welcome to geeks help."
newString = myString.capitalize()
print (newString)


2. casefold()

This method is used to convert the string into lowercase.

myString = "Hey, Welcome to Geeks Help."
newString = myString.casefold()
print (newString)


3. center()

This Python string method returns a centered string.

myString = "geeks help."
newString = myString.center(10)
print (newString)


4. encode()

This Python string method returns an encoded version of the string.

myString = "Geeks Help."
newString = myString.encode()
print (newString)


5. endswith()

This string method will return true if the given string ends with the specified value.

myString = "Geeks Help."
newString = myString.endswith(".")
print (newString)


6. expandtabs()

This Python string method will set the tab size of the string.

myText = "G\te\te\tk\ts"
myString =  myText.expandtabs(2)
print(myString)

7. find()

This method searches the string for a specified value and it will return the position of where it was found.

myText = "Hello, Welcome to Geeks Help"
myString =  myText.find("Welcome")
print(myString)


8. format()

This method in Python string is used to format specified value in a string.

myText = "Geeks Help HTML Notes {price:.1f} rupyees"
myString =  myText.format(price = 59)
print(myString)


9. isalnum()

This Python string method will return true if all the characters in the string are alphanumeric.

myText = "Geeks Help2023"
myString =  myText.isalnum()
print(myString)


10. isalpha()

This Python string method will return true if all the characters in the string are alphabet.

myText = "GeeksHelp"
myString =  myText.isalpha()
print(myString)


11. isascii()

This Python string method will return true if all the characters in the string are ascii characters.

myText = "GeeksHelp1234"
myString =  myText.isascii()
print(myString)


12. isdecimal()

This Python string method will return true if all the characters in the string are decimals.

myText = "73573"
myDecimal =  myText.isdecimal()
print(myDecimal)


13. isdigit()

This Python string method will return true if all the characterscter in the string are digit.

myText = "73573"
myDigit =  myText.isdigit()
print(myDigit)


12. isidentifier()

This Python string method will return true if the string is an identifier.

myText = "GeeksHelp"
myIdentifier =  myText.isidentifier()
print(myIdentifier)


13. islower()

This Python string method will return true if all the characters in the string are lowercase.

myText = "geeks"
lowerString =  myText.islower()
print(lowerString)

14. isnumeric()

This Python string method will return true if all the characters in the string are numeric.

myText = "123456"
myNumeric =  myText.isnumeric()
print(myNumeric)


15. isprintable()

This Python string method will return true if all the characters in the string are printable.

myText = "Hey! have you achieved #1 position?"
printableText =  myText.isprintable()
print(printableText)


16. isspace()

This Python string method will return true if all the characters in the string are whitespaces.

myText = "                  "
checkSpace =  myText.isspace()
print(checkSpace)


17. isupper()

This Python string method will return true if all the characters in the string are upper case.

myText = "WELCOME TO GEEKS HELP"
myUpperText =  myText.isupper()
print(myUpperText)


18. join()

This string method will convert the elements of an iterable into a string.

myText = ("HTML", "CSS", "JavaScript")
joinedText =  "#".join(myText)
print(joinedText)


19. ljust()

This Python string method will return a left-justified version of the string.

myText = "GeeksHelp"
myString  = myText.ljust(22)
print(myString, "is my favorite website")


20. lower()

This Python string method will convert a string into lower case.

myText = "HELLO Everyone WELCOME TO Geeks Help"
myString  = myText.lower()
print(myString)


21. lstrip()

This Python string method will return a left-trim version of the string.

myText = "        Geeks Help        "
myString  = myText.lstrip()
print(myString)


22. replace()

This string method will return a string where the specified value is replaced with a specified value.

myText = "I want Job Updates"
myString  = myText.replace("Job", "Internship")
print(myString)


23. rstrip()

This Python string method will returns a right-trim version of a string.

myText = "        Geeks Help        "
myString  = myText.rstrip()
print(myString)

24. split()

This Python string method will splits the string at the specified separator and return a list.

myText = "Welcome to Geeks Help. A educational platform for Computer Science Students"
myString  = myText.split()
print(myString)


25. startswith()

This string method will return true if the given string starts with the specified value.

myString = "Geeks Help."
newString = myString.startswith("Geeks")
print (newString)


26. strip()

This method will return a trimmed version of the string.

myString = "              Geeks Help.              "
newString = myString.strip()
print ("From all educational websites", newString, "is my favorite")


27. title()

This Python string method will convert the first character of each word to upper case.

myString = "Welcome to Geeks Help educational world."
newString = myString.title()
print(newString)


28. upper()

This string method will convert a string into upper case.

myString = "Welcome to Geeks Help educational world."
newString = myString.upper()
print(newString)


29. translate()

Modify the string according to the translation mappings provided.

#use a dictionary with ascii codes to replace 83 (S) with 80 (P):
myDictionary = {80: 83}
myString = "Geeks Help is Pchool of education world."
newString = myString.translate(myDictionary)
print(newString)


30. zfill()

This Python string method will return a copy of the string with '0' characters padded at the beginning to the string.

myString = "20"
newString = myString.zfill(5)
print(newString)


31. swapcase()

This Python string method will convert all uppercase characters to lowercase and vice versa.

myString = "hello this is gEEKS hELP"
newString = myString.swapcase()
print(newString)


32. istitle()

This Python string method will return true if the string is titled properly otherwise it will return false.

myText = "Welcome to Geeks Help"
myTitle =  myText.istitle()
print(myTitle)


33. len()

This Python string method will return the length of a string.

myString = "Geeks Help"
print("Length of Geeks Help is: ", len(myString))


String Methods in Python with Examples PDF: Download Now

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad