W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/20/2021

Python String

 In Python, a string literal is:

  • an array of bytes representing unicode characters
  • surrounded by either single quotation marks, or double quotation marks
  • of unlimited length
string literals
str = 'hello world'
 
str = "hello world"

multi-line string is created using three single quotes or three double quotes.

multi-line string literals
str = '''Say hello
        to python
        programming'''
 
str = """Say hello
        to python
        programming"""

Python does not have a character data type, a single character is simply a string with a length of 1.

2. Substring or slicing

We can get a range of characters by using the slice syntax. Indexes start from zero.

str[m:n] returns string from position 2 (including) to 5 (excluding).

substring from index 2 to 5
str = 'hello world'
 
print(str[2:5]) # llo

Negative slicing returns the substring from the end.

substring from index -5 to -2
str = 'hello world'
 
print(str[-5:-2])   # wor

str[-m:-n] returns string from position -5 (excluding) to -2 (including).

3. Strings as arrays

In python, strings behave as arrays. Square brackets can be used to access elements of the string.

character at nth position
str = 'hello world'
 
print(str[0])   # h
print(str[1])   # e
print(str[2])   # l
 
print(str[20])  # IndexError: string index out of range

4. String length

The len() function returns the length of a string:

String length
str = 'hello world'
 
print(len(str)) # 11

5. String Formatting

To format s string in python, use placeholders { } in string at desired places. Pass arguments to format() function to format the string with values.

We can pass the argument position in placeholders (starting with zero).

String format()
age = 36
name = 'Lokesh'
 
txt = "My name is {} and my age is {}"
 
print(txt.format(name, age))    # My name is Lokesh and my age is 36
 
txt = "My age is {1} and the name is {0}"
 
print(txt.format(name, age))    # My age is 36 and the name is Lokesh

6. String Methods

6.1. capitalize()

It returns a string where the very first character of given string is converted to UPPER CASE. When first character is non-alphabet, it returns the same string.

String capitalize()
name = 'lokesh gupta'
 
print( name.capitalize() )  # Lokesh gupta
 
txt = '38 yrs old lokesh gupta'
 
print( txt.capitalize() )   # 38 yrs old lokesh gupta

6.2. casefold()

It returns a string where all the characters are lower case of a given string.

String casefold()
txt = 'My Name is Lokesh Gupta'
 
print( txt.casefold() ) # my name is lokesh gupta

6.3. center()

It center align the string, using a specified character (space is default) as the fill character.

In given example, output takes total 20 characters and “hello world” is in the middle of it.

String center()
txt = "hello world"
 
x = txt.center(20)
 
print(x)    # '    hello world     '

6.4. count()

It returns the number of times a specified value appears in the string. It comes in two forms:

    count(value) - value to search for in the string.
    count(value, start, end) - value to search for in the string, where search starts from start position till end position.
String count()
txt = "hello world"
 
print( txt.count("o") )         # 2
 
print( txt.count("o", 4, 7) )   # 1

6.5. encode()

It encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.

String encode()
txt = "My name is åmber"
 
x = txt.encode()
 
print(x)    # b'My name is \xc3\xa5mber'

6.6. endswith()

It returns True if the string ends with the specified value, otherwise False.

String endswith()
txt = "hello world"
 
print( txt.endswith("world") )      # True
 
print( txt.endswith("planet") )     # False

6.7. expandtabs()

It sets the tab size to the specified number of whitespaces.

String expandtabs()
txt = "hello\tworld"
 
print( txt.expandtabs(2) )      # 'hello world'
 
print( txt.expandtabs(4) )      # 'hello   world'
 
print( txt.expandtabs(16) )     # 'hello           world'

6.8. find()

It finds the first occurrence of the specified value. It returns -1 if the specified value is not foun in the string.

find() is same as the index() method, only difference is that the index() method raises an exception if the value is not found.

String find()
txt = "My name is Lokesh Gupta"
 
x = txt.find("e")
 
print(x)        # 6

6.9. format()

It formats the specified string and insert argument values them inside the string’s placeholders.

String format()
age = 36
name = 'Lokesh'
 
txt = "My name is {} and my age is {}"
 
print( txt.format(name, age) )  # My name is Lokesh and my age is 36

6.10. format_map()

It is used to return an dictionary key’s value to format a string with named placeholders.

String format_map()
params = {'name':'Lokesh Gupta', 'age':'38'}
 
txt = "My name is {name} and age is {age}"
 
x = txt.format_map(params)
 
print(x)        # My name is Lokesh Gupta and age is 38

6.11. index()

  • It finds the first occurrence of the specified value in given string.
  • It raises an exception if the value to be serached is not found.
String index()
txt = "My name is Lokesh Gupta"
 
x = txt.index("e")
 
print(x)        # 6
 
x = txt.index("z")  # ValueError: substring not found

6.12. isalnum()

It checks a alphanumeic string. It returns True if all the characters are alphanumeric, meaning alphabet letter (a-zA-Z) and numbers (0-9).

String isalnum()
print("LokeshGupta".isalnum())      # True
 
print("Lokesh Gupta".isalnum())     # False - Contains space

6.13. isalpha()

It returns True if all the characters are alphabets, meaning alphabet letters (a-zA-Z).

String isalpha()
print("LokeshGupta".isalpha())          # True
 
print("Lokesh Gupta".isalpha())         # False - Contains space
 
print("LokeshGupta38".isalpha())        # False - Contains numbers

6.14. isdecimal()

It returns code if all the characters are decimals (0-9). Else returns False.

String isdecimal()
print("LokeshGupta".isdecimal())    # False
 
print("12345".isdecimal())          # True
 
print("123.45".isdecimal())         # False - Contains 'point'
 
print("1234 5678".isdecimal())      # False - Contains space

6.15. isdigit()

It returns True if all the characters are digits, otherwise False. Exponents are also considered to be a digit.

String isdigit()
print("LokeshGupta".isdigit())      # False
 
print("12345".isdigit())            # True
 
print("123.45".isdigit())           # False - contains decimal point
 
print("1234\u00B2".isdigit())       # True - unicode for square 2

6.16. isidentifier()

It returns True if the string is a valid identifier, otherwise False.

A valid identifier only contains alphanumeric letters (a-z) and (0-9), or underscores ( _ ). It cannot start with a number, or contain any spaces.

String isidentifier()
print( "Lokesh_Gupta_38".isidentifier() )       # True
 
print( "38_Lokesh_Gupta".isidentifier() )       # False - Start with number
 
print( "_Lokesh_Gupta".isidentifier() )         # True
 
print( "Lokesh Gupta 38".isidentifier() )       # False - Contain spaces

6.17. islower()

It returns True if all the characters are in lower case, otherwise False. Numbers, symbols and spaces are not checked, only alphabet characters.

String islower()
print( "LokeshGupta".islower() )        # False
 
print( "lokeshgupta".islower() )        # True
 
print( "lokesh_gupta".islower() )       # True
 
print( "lokesh_gupta_38".islower() )    # True

6.18. isnumeric()

It method returns True if all the characters are numeric (0-9), otherwise False. Exponents are also considered to be numeric values.

String isnumeric()
print("LokeshGupta".isnumeric())    # False
 
print("12345".isnumeric())          # True
 
print("123.45".isnumeric())         # False - contains decimal point
 
print("1234\u00B2".isnumeric())     # True - unicode for square 2

6.19. isprintable()

It returns True if all the characters are printable, otherwise False. Non-printable characters are used to indicate certain formatting actions, such as:

  • White spaces (considered an invisible graphic)
  • Carriage returns
  • Tabs
  • Line breaks
  • Page breaks
  • Null characters
String isprintable()
print("LokeshGupta".isprintable())      # True
 
print("Lokesh Gupta".isprintable())     # True
 
print("Lokesh\tGupta".isprintable())    # False

6.20. isspace()

It returns True if all the characters in a string are whitespaces, otherwise False.

6.21. istitle()

It returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, i.e. Title Case. Otherwise False.

String istitle()
print("Lokesh Gupta".istitle())     # True
 
print("Lokesh gupta".istitle())     # False

6.22. isupper()

It returns True if all the characters are in upper case, otherwise False. Numbers, symbols and spaces are not checked, only alphabet characters.

String isupper()
print("LOKESHGUPTA".isupper())      # True
 
print("LOKESH GUPTA".isupper())     # True
 
print("Lokesh Gupta".isupper())     # False

6.23. join()

It takes all items in an iterable and joins them into one string using the mandatory specified separator.

String join()
myTuple = ("Lokesh", "Gupta", "38")
 
x = "#".join(myTuple)
 
print(x)    # Lokesh#Gupta#38

6.24. ljust()

This method will left align the string, using a specified character (space is default) as the fill character.

String ljust()
txt = "lokesh"
 
x = txt.ljust(20, "-")
 
print(x)    # lokesh--------------

6.25. lower()

It method returns a string where all characters are lower case. Symbols and Numbers are ignored.

String lower()
txt = "Lokesh Gupta"
 
x = txt.lower()
 
print(x)    # lokesh gupta

6.26. lstrip()

It method removes any leading characters (space is the default).

String lstrip()
txt = "#Lokesh Gupta"
 
x = txt.lstrip("#_,.")
 
print(x)    # Lokesh Gupta

6.27. maketrans()

It creates a one to one mapping of a character to its translation/replacement. This translation mapping is then used for replacing a character to its mapped character when used in translate() method.

String maketrans()
dict = {"a": "123", "b": "456", "c": "789"}
 
string = "abc"
 
print(string.maketrans(dict))   # {97: '123', 98: '456', 99: '789'}

6.28. partition()

It searches for a specified string in given text, and splits the string into a tuple containing three elements:

  • The first element contains the part before the specified string.
  • The second element contains the specified string.
  • The third element contains the part after the string.
String partition()
txt = "my name is lokesh gupta"
 
x = txt.partition("lokesh")
 
print(x)    # ('my name is ', 'lokesh', ' gupta')
 
print(x[0]) # my name is
print(x[1]) # lokesh
print(x[2]) #  gupta

6.29. replace()

It replaces a specified phrase with another specified phrase. It comes in two forms:

  • string.replace(oldvalue, newvalue)
  • string.replace(oldvalue, newvalue, count) – ‘count’ specifies how many occurrences you want to replace. Default is all occurrences.
String replace()
txt = "A A A A A"
 
x = txt.replace("A", "B")
 
print(x)    # B B B B B
 
x = txt.replace("A", "B", 2)
 
print(x)    # B B A A A

6.30. rfind()

It finds the last occurrence of the specified value. It returns -1 if the value is not found in given text.

String rfind()
txt = "my name is lokesh gupta"
 
x = txt.rfind("lokesh")    
 
print(x)        # 11
 
x = txt.rfind("amit")      
 
print(x)        # -1

6.31. rindex()

It finds the last occurrence of the specified value and raises an exception if the value is not found.

String rindex()
txt = "my name is lokesh gupta"
 
x = txt.rindex("lokesh")       
 
print(x)                # 11
 
x = txt.rindex("amit")  # ValueError: substring not found

6.32. rjust()

It will right align the string, using a specified character (space is default) as the fill character.

String rjust()
txt = "lokesh"
 
x = txt.rjust(20,"#")
 
print(x, "is my name")  # ##############lokesh is my name

6.33. rpartition()

It searches for the last occurrence of a specified string, and splits the string into a tuple containing three elements.

  • The first element contains the part before the specified string.
  • The second element contains the specified string.
  • The third element contains the part after the string.
String rpartition()
txt = "my name is lokesh gupta"
 
x = txt.rpartition("lokesh")
 
print(x)    # ('my name is ', 'lokesh', ' gupta')
 
print(x[0]) # my name is
print(x[1]) # lokesh
print(x[2]) #  gupta

6.34. rsplit()

It splits a string into a list, starting from the right.

String rsplit()
txt = "apple, banana, cherry"
 
x = txt.rsplit(", ")
 
print(x)    # ['apple', 'banana', 'cherry']

6.35. rstrip()

It removes any trailing characters (characters at the end a string), space is the default trailing character.

String rstrip()
txt = "     lokesh     "
 
x = txt.rstrip()
 
print(x)    # '     lokesh'

6.36. split()

It splits a string into a list. You can specify the separator. The default separator is whitespace.

String split()
txt = "my name is lokesh"
 
x = txt.split()
 
print(x)    # ['my', 'name', 'is', 'lokesh']

6.37. splitlines()

It splits a string into a list, by splitting at line breaks.

String splitlines()
txt = "my name\nis lokesh"
 
x = txt.splitlines()
 
print(x)    # ['my name', 'is lokesh']

6.38. startswith()

It returns True if the string starts with the specified value, otherwise False. String comparison is case-sensitive.

String startswith()
txt = "my name is lokesh"
 
print( txt.startswith("my") )   # True
 
print( txt.startswith("My") )   # False

6.39. strip()

It removes all leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default).

String strip()
txt = "   my name is lokesh   "
 
print( txt.strip() )    # 'my name is lokesh'

6.40. swapcase()

It returns a string where all the upper case letters are lower case and vice versa.

String swapcase()
txt = "My Name Is Lokesh Gupta"
 
print( txt.swapcase() ) # mY nAME iS lOKESH gUPTA

6.41. title()

It returns a string where the first character in every word is upper case. If the word contains a number or a symbol in start, the first letter after that will be converted to upper case.

String title()
print( "lokesh gupta".title() ) # Lokesh Gupta
 
print( "38lokesh gupta".title() )   # 38Lokesh Gupta
 
print( "1. lokesh gupta".title() )  # Lokesh Gupta

6.42. translate()

It takes the translation table to replace/translate characters in the given string as per the mapping table.

String translate()
translation = {97: None, 98: None, 99: 105}
 
string = "abcdef"  
 
print( string.translate(translation) )  # idef

6.43. upper()

It returns a string where all characters are in upper case. Symbols and Numbers are ignored.

String upper()
txt = "lokesh gupta"
 
print( txt.upper() )    # LOKESH GUPTA

6.44. zfill()

It adds zeros (0) at the beginning of the string, until it reaches the specified length.

String zfill()
txt = "100"
 
x = txt.zfill(10)
 
print( 0000000100 ) # 0000000100

Happy Learning !!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.