Python String

What is Python String?

A string in python is just an array of characters.

The important properties of Python strings are as follows:

  • Strings are unchangeable (immutable) – They can’t be changed in place.
  • Supports Unicode characters – The default encoding for Python source code is UTF-8
  • Strings are ordered – Strings remember the order of characters inserted.
  • Accessed by index – Individual characters in a string can be accessed using an index.

Create a String

A python string is zero or more characters written inside single quotes ' ' or double quotes " "

Example:

S = 'Hello, World!'   # single quotes
S = "Hello, World!"   # double quotes

You can use quotes inside a string, as long as they don’t match the quotes surrounding the string.

Example: Using quotes inside a string in python

S = "We're open"		# Escape single quote
S = "I said 'Wow!'"		# Escape single quotes
S = 'I said "Wow!"'		# Escape double quotes

Multiline Strings

You can create a multiline string using triple-quotes: """  """ or '''  '''.

Example: Create a multiline string using triple-quotes

S = """String literals can
span multiple lines."""
print(S)
# String literals can
# span multiple lines.

The str() Constructor

You can convert almost any object in Python to a string using a type constructor called str()

Example: Convert objects to a string

S = str(42)
print(S)    # '42'
S = str(3+4j)
print(S)    # '(3+4j)'

Even a list

S = str([1,1])
print(S)    # '[1, 1]'

Access Characters by Index

You can access individual characters in a string using an index in square brackets. The string indexing starts from 0.

You can also access a string by negative indexing. A negative string index counts from the end of the string.

The indices for the elements in a string are illustrated as below:

String Indexing

Example: Access individual characters in a string

# Indexing
S = 'ABCDEFGHI'
print(S[0])     # A
print(S[4])     # E
# Negative Indexing
S = 'ABCDEFGHI'
print(S[-1])    # I
print(S[-6])	# D

Slicing a String

In python, you can extract substrings by slicing a string.

Example: Extract substrings by slicing a string

S = 'ABCDEFGHI'
print(S[2:5])	  # CDE
print(S[5:-1])	  # FGH
print(S[1:6:2])   # BDF

The string slicing capability provided by python is extensive and covered in full detail here.

Modify a String

Strings are unchangeable (immutable). Once a string is created, it cannot be modified.

Example: Strings are unchangeable

# TypeError: 'str' object does not support item assignment
S = 'Hello, World!'
S[1] = 'i'
# TypeError: 'str' object doesn't support item deletion
S = 'Hello, World!'
del S[1]

String Concatenation

You can concatenate strings using the concatenation operator + or the augmented assignment operator +=

Example: Concatenate two strings

S = 'Hello,' + ' World!'
print(S)
# Hello, World!
S = 'Hello,'
S += ' World!'
print(S)    # Hello, World!

Two or more strings, next to each other are automatically concatenated.

Example: Implicit concatenation in python

S = 'Hello,' " World!"
print(S)    # Hello, World!

Implicit concatenation only works with two literals though, not with variables or expressions.

You can put several strings within parentheses to join them together. This feature is useful when you want to break long strings.

Example: Break & concatenate long strings

S = ('Put strings within parentheses '
    'to join them together.')
print(S)
# Put strings within parentheses to join them together.

You can replicate substrings in a string using the replication operator *

Example: Replicate substrings in a string

# the hard way
S = '--------------------'

# the easy way
S = '-' * 20

Find String Length

To find how many characters a string has, use len() built-in function.

Example: Find length of a string

S = 'Supercalifragilisticexpialidocious'
print(len(S))   # 34

Replace Text Within a String

To replace text within a string, use replace() method.

Example: Replace ‘World’ with ‘Universe’

S = 'Hello, World!'
x = S.replace('World', 'Universe')
print(x)    # Hello, Universe!

Split and Join a String

Use split() method to chop up a string into a list of substrings, around a specified delimiter.

Example: Split the string on comma

S = 'red,green,blue,yellow'
x = S.split(',')
print(x)    	# ['red', 'green', 'blue', 'yellow']
print(x[0])		# red

use join() method to join the list back into a string, with a specified delimiter in between.

Example: Join the list of substrings

L = ['red', 'green', 'blue', 'yellow']
S = ','.join(L)
print(S)    # red,green,blue,yellow

String Case Conversion

Python provides five methods to perform case conversion on the target string viz. lower(), upper(), capitalize(), swapcase() and title()

Examples:

S = 'Hello, World!'
print(S.lower())       # hello, world!
S = 'Hello, World!'
print(S.upper())       # HELLO, WORLD!
S = 'Hello, World!'
print(S.capitalize())	# Hello, world!
S = 'Hello, World!'
print(S.swapcase())		# hELLO, wORLD!
S = 'hello, world!'
print(S.title())		# Hello, World!

Check if Substring Contains in a String

To check if a specific text is present in a string, use in and not in operators with if statement.

Example: Check for presence/absence of a substring

# Check for presence
S = 'Hello, World!'
if 'Hello' in S:
    print('Yes')
# Check for absence
S = 'Hello, World!'
if 'Universe' not in S:
    print('Yes')

To search for a specific text within a string, use find() method. It returns the lowest index in the string where substring is found.

Example: Search for ‘Foolish’ within a string

S = 'Stay Hungry, Stay Foolish'
x = S.find('Foolish')
print(x)    # 18

Iterate Through a String

To iterate over the characters of a string, use a simple for loop.

Example: Print each character in a string

S = 'Hello, World!'
for letter in S:
    print(letter, end=' ')
# H e l l o ,   W o r l d ! 

Python Escape Sequence

Suppose you want to declare a string with both single and double quotes like:

Bob told me, “Sam said, ‘This won’t work.'”

Python will raise a SyntaxError, because both quotation marks are special characters.

The solution to avoid this problem is to use the backslash escape character \.

Prefixing a special character with \ turns it into an ordinary character. This is called escaping.

Example: Escape special characters in Python

# Escape single quote
S = str('won\'t')
print(S)    # doesn't
# Escape double quote
S = str("\"Yes\", they said.")
print(S)    # "Yes", they said.
# Escape single and double quotes
S = str('"Isn\'t", they said.')
print(S)    # "Isn't", they said.

Backslash escape character is used in representing certain special characters like: \n is a newline, \t is a tab. These are known as escape sequences.

Example:

S = str('First line.\n\tSecond line.')
print(S)
# First line.
#     Second line.

A list of all valid escape sequences in Python:

Escape SequenceMeaning
\newlineBackslash and newline ignored
\\Backslash (\)
\’Single quote (‘)
\”Double quote (“)
\aASCII Bell (BEL)
\bASCII Backspace (BS)
\fASCII Formfeed (FF)
\nASCII Linefeed (LF)
\rASCII Carriage Return (CR)
\tASCII Horizontal Tab (TAB)
\vASCII Vertical Tab (VT)
\oooCharacter with octal value ooo
\xhhCharacter with hex value hh

Raw String

If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote.

Example:

S = 'C:\new\text.txt'
print(S)
# C:
# ew	ext.txt

Here, \n is interpreted as newline and \t as tab. To avoid this, add r before the first quote to make it raw string.

Example:

S = r'C:\new\text.txt'
print(S)
# C:\new\text.txt

String Formatting/String Interpolation

In Python, there are three major ways to embed variables inside a string.

  • printf-style % String Formatting
  • str.format() Built-in Method
  • f-String Formatter

Example: Use printf-style % string formatting

S = '%s is %d years old.' % ('Bob', 25)
print(S)    # Bob is 25 years old.

Example: Use format() Built-in Method

S = '{1} is {0} years old.'.format(25, 'Bob')
print(S)	# Bob is 25 years old.

The format() Built-in Method is covered in full detail here.

Example: Use f-String Formatter

name = 'Bob'
age = 25
S = f"{name} is {age} years old."
print(S)	# Bob is 25 years old.

Python String Methods

Python has a set of built-in methods that you can call on string objects.

Python Steing Methods
MethodDescription
capitalize()Capitalizes first character of the string
casefold()Returns a casefolded string
center()Returns center-aligned string
count()Counts occurrences of a substring in a string
encode()Return an encoded version of the string as a bytes object
endswith()Determines whether the string ends with a given suffix
expandtabs()Replaces tabs with spaces
find()Searches the string for a given substring
format()Perform a string formatting operation
format_map()Perform a string formatting operation
index()Searches the string for a given substring
isalnum()Determines whether the string contains alphanumeric characters
isalpha()Determines whether the string contains alphabetic characters
isdecimal()Determines whether the string contains decimal characters
isdigit()Determines whether the string contains digits
isidentifier()Determines whether the string is a valid Python identifier
islower()Determines whether string contains lowercase characters
isnumeric()Determines whether the string contains numeric characters
isprintable()Determines whether string contains printable characters
isspace()Determines whether the string contains only whitespace characters
istitle()Determines whether the string is a titlecased string
isupper()Determines whether string contains uppercase characters
join()Joins all items in an iterable into a single string
ljust()Returns left-aligned string
lower()Converts all characters in a string to lowercase
lstrip()Strips characters from the left end of a string
maketrans()Returns a translation table to be used in translations
partition()Divides a string based on a separator
replace()Replaces occurrences of a substring within a string
rfind()Searches the string for a given substring
rindex()Searches the string for a given substring
rjust()Returns right-aligned string
rpartition()Divides a string based on a separator
rsplit()Splits a string into a list of substrings
rstrip()Strips characters from the right end of a string
split()Splits a string into a list of substrings
splitlines()Splits the string at line breaks
startswith()Determines whether the string starts with a given substring
strip()Strips leading and trailing characters
swapcase()Swaps case of all characters in a string
title()Converts string to “Title Case”
translate()Returns a translated string
upper()Converts all characters in a string to uppercase
zfill()Pads a string on the le with zeros