Strings

What are Strings?

In Python, a string is a sequence of Unicode characters enclosed within single ('), double ("), or triple quotes (''' or """). Triple quotes are particularly useful for multi-line strings. Strings are immutable, meaning once created, their contents cannot be changed. However, you can create new strings based on existing ones.

# Single-line strings
single = 'Hello'
double = "World"

# Multi-line string
multi = """This is
a multi-line
string."""

Concatenation and Formatting

You can concatenate strings using the + operator.

greeting = "Hello, " + "World!"
print(greeting)  # Output: Hello, World!

Introduced in Python 3.6, formatted string literals (f-strings) allow embedding expressions inside string literals. The syntax is to put an f outside the opening quotation mark of a string and to insert variables into the string wrapped in curly brackets.

name = "Adam"
age = 34
print(f"{name} is {age} years old.")  # Output: Adam is 34 years old.

You can include special characters in a string such as quotation marks by preceding them with the escape character \.

You can also use the string method .format(). It takes parameters as arguments and includes them in a string.

def favourite_song_statement(song, artist):
    return "My favourite song is {} by {}.".format(song, artist)

Accessing and Slicing Strings

Strings in Python can be though of as a list of characters. Like in JavaScript, Python is zero index, so the first letter in a string will have the index of 0 and the index of the last letter is the length of the string minus 1.

text = "Python"
print(text[0])    # Output: 'P'
print(text[-1])   # Output: 'n'
print(text[len(text)-1]) # Output: 'n'
print(text[1:4])  # Output: 'yth'

The length of a string can be determined using len().

len(string) # Gives the length of the string

Common String Methods

Methods in Python also follow dot notation so will take the syntax of variable.method(parameter).

Case Conversion

  • lower(): Converts to lowercase.
  • upper(): Converts to uppercase.
  • capitalize(): Capitalises the first character.
  • title(): Capitalizes the first character of each word.
  • swapcase(): Swaps case of each character.

Searching and Replacing

  • count(): Returns the amount of times a specified value occurs.
  • endswith(): Returns True if the string ends with a given suffix.
  • find(sub): Returns the lowest index of substring sub; returns -1 if not found.
  • rfind(sub): Returns the highest index of substring sub; returns -1 if not found.
  • index(sub): Like find(), but raises a ValueError if sub is not found.
  • replace(old, new): Replaces occurrences of old with new.

Trimming and Padding

  • expandstabs(): Sets the tab size in spaces of the string.
  • strip(): Removes leading and trailing whitespace.
  • lstrip(): Removes leading whitespace.
  • rstrip(): Removes trailing whitespace.
  • center(width, fillchar): Centers string in a field of given width, padded with fillchar.
  • ljust(width, fillchar): Left-justifies string.
  • rjust(width, fillchar): Right-justifies string.

Splitting and Joining

  • partition(): Returns a tuple where the string is parted into two strings and the separator.
  • split("delimiter"): Splits string into a list using a given delimiter. By default it uses a space.
  • rpartition(): Returns a tuple where the string is parted into three parts.
  • rsplit(sep): Splits string from the right.
  • splitlines(): Splits string at line breaks.
  • 'delimiter'.join(iterable): Joins elements of iterable with a given delimiter.

Validation Methods

  • isalnum(): Checks if all characters are alphanumeric.
  • isalpha(): Checks if all characters are alphabetic.
  • isdigit(): Checks if all characters are digits.
  • islower(): Checks if all characters are lowercase.
  • isupper(): Checks if all characters are uppercase.
  • isspace(): Checks if all characters are whitespace.
  • istitle(): Checks if string is titlecased.

Encoding and Decoding

  • encode(encoding): Encodes string using specified encoding.
  • decode(encoding): Decodes bytes to string using specified encoding.