Python Strings

Larn about Python string datatype and its functions. Learn how to format, align, find length and other such useful programs and exercises.

Python

Learn about strings in Python and how to perform various operations on the strings with simple examples.

1. Creating a 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
str = 'hello world'

str = "hello world"

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

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 of length 1.

2. Substring or Slicing

We can get a range of characters by using the slice syntax. Indexes start from 0. For example, str[m:n] returns a string from position 2 (including) to 5 (excluding).

str = 'hello world'

print(str[2:5])	# llo

Negative slicing returns the substring from the end. For example, str[-m:-n] returns string from position -5 (excluding) to -2 (including).

str = 'hello world'

print(str[-5:-2])	# wor

3. String as an Array

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

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:

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).

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 the first character is non-alphabet, it returns the same string.

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 lowercase of a given string.

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 the default) as the fill character.

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

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 the search starts from start position till end position.
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.

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.

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.

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 found 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.

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 inside the string’s placeholders.

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 returns a dictionary key’s value to format a string with named placeholders.

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 the given string.
  • It raises an exception if the value to be searched is not found.
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 an alphanumeric string. It returns True if all the characters are alphanumeric, meaning alphabet letters (a-zA-Z) and numbers (0-9).

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).

print("LokeshGupta".isalpha())			# True

print("Lokesh Gupta".isalpha())			# False - Contains space

print("LokeshGupta38".isalpha())		# False - Contains numbers

6.14. isdecimal()

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

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.

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.

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.

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.

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
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.

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.

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.

myTuple = ("Lokesh", "Gupta", "38")

x = "#".join(myTuple)

print(x)	# Lokesh#Gupta#38

6.24.