Skip to main content

Command Palette

Search for a command to run...

Python String Methods

This article comprises of all the methods that Python can use for strings

Updated
4 min readView as Markdown
Python String Methods
N

I am a developer who is very enthusiast about technology and coding.

String is an immutable sequence data type.

It is the sequence of Unicode characters wrapped inside single, double, or triple quotes.

Functions

  1. capitalize()

    • Converts the first character to upper case

      txt = "hello world!"
      x = txt.capitalize()
      print(x)
      
  2. casefold()

    • Converts string into lower case

      txt = "Hello, World!"
      x = txt.casefold()
      print(x)
      
  3. center()

    • Returns a centered string

      txt = "mercedes"
      x = txt.center(10)
      print(x)
      
  4. count()

    • Returns the number of times a specified value occurs in a string

      txt = "I love buggatti, buggatti is my favourite company."
      x = txt.count("buggatti")
      print(x)
      
  5. encode()

    • Returns an encoded version of the string

      txt = "I love buggatti, buggatti is my favourite company."
      x = txt.encode()
      print(x)
      
  6. endswith()

    • Returns true if the string ends with specified value

      txt = "I love buggatti, buggatti is my favourite company."
      x = txt.endswith(".")
      print(x)
      
  7. expandtabs()

    • Sets the tab size of the string

      txt = "I love buggatti, buggatti is my favourite company."
      x = txt.expandtabs(3)
      print(x)
      
  8. find()

    • Searches the string for a specified value and returns the position of where it was found

      txt = "I love buggatti, buggatti is my favourite company."
      x = txt.find("buggatti")
      print(x)
      
  9. format()

    • Formats specified values in a string

      txt = "For only {price:.2f} dollars!"
      print(txt.format(price = 49))
      
  10. join()

    • Convers the elements of an iterable into a string
    company = ("Mercedes", "Buggatti", "Pagani")
    x = "vs".join(company)
    print(x)
    
  11. lower()

    • Converts string into lower case
    txt = "HELLO, WORLD!"
    x = txt.lower()
    print(x)
    
  12. maketrans()

    • Returns a translation table to be used in translations
    txt = "HELLO, WORLD!"
    x = txt.maketrans("WORLD", "PEOPLE"))
    print(x)
    
  13. replace()

    • Returns a string where a specified value is replaced with a specified value
    txt = "HELLO, WORLD!"
    x = txt.replace("WORLD", "PEOPLE"))
    print(x)
    
  14. split()

    • Splits the string at the specified separator, and returns a list
    txt = "Welcome to my world!"
    x = txt.split()
    print(x)
    
  15. splitlines()

    • Splits the string at line breaks and returns a list
    txt = "Welcome to my world!"
    x = txt.splitlines()
    print(x)
    
  16. startswith()

    • Returns True if the string starts with the specified value
    txt = "Welcome to my world!"
    x = txt.startswith("Welcome")
    print(x)
    
  17. strip()

    • Returns a trimmed version of the string
    txt = "    People   "
    x = txt.strip()
    print("Welcome", x)
    
  18. swapcase()

    • Swap cases, lower case becomes upper case and vice versa
    txt = "Welcome to my WORLD!"
    x = txt.swapcase()
    print(x)
    
  19. title()

    • Converts the first character of each word to upper case
    txt = "welcome to my world!"
    x = txt.title()
    print(x)
    
  20. upper()

    • Converts a string into upper case
    txt = "Welcome to my world!"
    x = txt.upper()
    print(x)
    

String Checker

  1. isalnum()

    • Returns True if all characters in the string are alphanumeric

      txt = "AstonMartin007"
      x = txt.isalnum()
      print(x)
      
  2. isalpha()

    • Returns True if all characters in the string are in the alphabet

      txt = "SpaceX"
      x = txt.isalpha()
      print(x)
      
  3. isascii()

    • Returns True if all characters in the string are ascii characters

      txt = "AstonMartin007"
      x = txt.isascii()
      print(x)
      
  4. isdecimal()

    • Returns True if all characters in the string are decimals

      txt = "\u0033" #unicode for 3
      x = txt.isdecimal()
      print(x)
      
  5. isdigit()

    • Returns True if all characters in the string are digits/numbers

      txt = "2580"
      x = txt.isdigit()
      print(x)
      
  6. isidentifier()

    • Returns True if the string is an identifier

      txt = "Demo"
      x = txt.isidentifier()
      print(x)
      
  7. islower()

    • Returns True if all characters in the string are lower case

      txt = "song"
      x = txt.islower()
      print(x)
      
  8. isprintable()

    • Returns True if all characters in the string are printable

      txt = "2580"
      x = txt.isprintable()
      print(x)
      
  9. isspace()

    • Returns True if all characters in the string are whitespaces

      txt = "  "
      x = txt.isspace()
      print(x)
      
  10. istitle()

    • Returns True if the string follows the rules of a title
    txt = "This is my code"
    x = txt.istitle()
    print(x)
    
  11. isupper()

    • Returns True if all characters in the string are upper case
    txt = "CODE"
    x = txt.isupper()
    print(x)
    

More from this blog

BigSmoke's Blog

44 posts

I am an enthusiast writer who is currently pursuing Petroleum Engineering but has passion in Coding. I want to share my thoughts and progress on coding while giving you new tips on coding cheat sheets