Strings

November 11, 2022

Some basic examples of creating strings in Python.

# Create a string with single quotes or double quotes
s1 = 'hello'
s2 = "hello there"

# Use \ to escape a single quote or use double quotes
s3 = 'that ain\'t the one'
s4 = "that ain't the one"

# Triple quotes can also be used
s5 = """another string"""

String methods

Methods available for the string type are demonstrated below.

# Join a list of words as a single string
>>> words = ['My', 'favorite', 'food', 'is', 'not', 'avocado']
>>> ' '.join(words)
'My favorite food is not avocado'

# Join a list of words excluding the `not` string
>>> ' '.join(filter(lambda x : x != 'not', words))
'My favorite food is avocado'
# Split a string into a list of strings
>>> 'hello there'.split(' ')
['hello', 'there']
# Replace the occurance of a string with another string
>>> 'hello there folks'.replace('there', 'all')
'hello all folks'
# Remove space at beginning and end of string
>>> ' space '.strip()
'space'
# Return a lowercase string
>>> 'Hello There'.casefold()
'hello there'
# Check if string starts with a given prefix
>>> 'hello there'.startswith('h')
True

# Check if string ends with a given suffix
>>> 'hello there'.endswith('k')
False
# Split lines into a list of strings
>>> """first line
    second line
    and third line""".splitlines()
['first line', 'second line', 'and third line']
# Count the occurance of a substring
>>> 'hello there'.count('e')
3
# Return a copy of the string where the first letter is capitalized
>>> s = 'i love apples'
>>> s.capitalize()
'I love apples'
# Return a copy of the string where words start with an uppercase
>>> s = 'i love apples'
>>> s.title()
'I Love Apples'
# Center a string using a fill character
>>> ' hello '.center(30, '*')
'*********** hello ************'
# Pad a numeric string with zeros
>>> '1'.zfill(3)
'001'

F-strings

>>> a = 2
>>> f'you have {a} apples'
'you have 2 apples'

>>> x = 99.4
>>> f'value is {x = }'
'value is x = 99.4'

Reverse a string

>>> name = 'Homer'
>>> name[::-1]
'remoH'

Unicode character

Refer to a unicode character by its name.

>>> "Hello snake \N{snake}"
'Hello snake 🐍'

Lookup the name associated with a unicode character.

>>> import unicodedata
>>> unicodedata.name('😀')
'GRINNING FACE'