Pt. 4 Python Strings

Python strings| Learning Python Pt. 4

Working with the string data type in Python, including string formatting, methods, prefixes & more

In this part of the "Learning Python" series, we're going to be exploring strings.

A string (also known as a string literal) is a sequence of characters contained between single, double or tripple quotes and is one of the Python data types.

Creating a string

Let's define a couple of variables and assign them both string values.

A string can be defined opened and closed with single quotes:

single_quotes = 'Awesome'

Or double quotes:

double_quotes = "Just as awesome"

However it's either one or the other:

syntax_error = "Not allowed'

The above example will cause a SyntaError:

  File "<stdin>", line 1
    syntax_error = "Not allowed'
                               ^
SyntaxError: EOL while scanning string literal

A string can contain any character (with a few workarounds which we'll cover shortly)

A string of numbers (Integer):

pin = "5463"

A string of decimal values (Float):

pi = "3.14"

Even if a string contains numbers, it's still a string.

A string can contain numbers and letters:

password = "MySecretPassword2468"

Or non alphanumeric characters:

non_alpha = "!£$$%^&*()_+={}[]@~#><?/|`¬"

Or a mixture of the above:

jibberish = "(bhyt!£$&*987)"

You may have noticed we've not included the backslash character \ as this serves a special purpose called escaping.

String escapes

Sometimes we need to make our strings behave differently.

Using escapes, we can provide some additional information inside a string which will change how they are evaluated.

We've discussed how you can create a string using single or double quotes, but what happens when we need to include single quotes inside of a string that's already been defined with single quotes?

For example, let's try to create a string using single quotes which includes an embedded single quote:

broken_string = 'How's about that?'

By including a single quote within our string, we're causing the string to be closed early and this will cause an error.

  File "strings.py", line 9
    broken_string = 'How's about that?'
                         ^
SyntaxError: invalid syntax

Python sees the inner quote as the closing quote, leaving the rest of the string outside of the quote!

We can get around this in a number of ways.

The simple way - Using double quotes to open and close the string:

fixed = "How's about that?"

Using a backslash \ just before the inner single quote to ESCAPE the inner quote:

also_fixed = 'How\'s about that?'

Using triple single or triple double quotes to open and close the string:

this_works = '''How's about that?'''
this_works_too = """How's about that?"""

Triple quotes also serve another purpose which we'll cover later.

The same principles also apply to string with an opening and closing set of double quotes:

message = "\"We need to escape\""
# Prints "We need to escape"

You'll see we've escaped multiple inner double quotes using 2 backslashes, each before the quotes we need to escape.

But what if you need a \ in your string?

If you try to include a single backslash before the closing quote, it will cause an error:

another_error = "This will fail \"

Python will see this as you're trying to escape the closing string! To avoid this, you can escape the backslash with another leading backslash just before it.

passable = "This will pass \\"

Escapes can be used for more than just dealing with inner quotes or backslashes, we can for example..

\n - ASCII linefeed (New line):

newline = "Split\nme\nup"
print(newline)

Will print:

Split
me
up

\a - ASCII bell:

ding_dong = "\a"
print(ding_dong)

Will print an empty line and sound a bell!

\b - ASCII backspace:

joined = "join \bme \bup" 
print(joined)

Returns:

joinmeup

As you can see, using escapes followed by a special character will cause the string to be evaluated and interpolated differently.

There are more escapes which we'll link at the end of the article!

Multi line strings

Sometimes we need a string to span more than just a single line.

Strings defined with single or double quotes cannot span multiple lines, however we can use triple quotes:

multi = """Multi
line
string"""

Is the same as:

multi = '''Multi
line
string'''

And both will return:

Multi
line
string

Any spaces or tabs inside a multi line string will be preserved, for example:

multi = '''
Multi
    line
        string
'''
print(multi)

Will print:

Multi
    line
        string

We can also use the backslash character to span a string over multiple lines, however it will be evaluated as a single line:

x = 'Splitting \
up \
a \
string'
print(x)

Prints:

Splitting up a string

The type() function

Sometimes we need to know what data type we're working with.

Just like the built in print() function, Python provides the type() funtion which will return the data type of whatever we pass it.

We use the type() function by passing it an argument, just like how we pass arguments to the print() function in between parenthesis.

Let's create a new string variable, pass it to the type() function, store the result as a variable and print the result:

my_string = "This is definitely a string"
data_type = type(my_string)
print(data_type)

The result:

<class 'str'>

The result tells us that the data type of my_string is str (short for string).

isinstance

Another useful built in function for when it comes to working with strings is isinstance(), a built in function that takes 2 arguments, a value and a data type.

Let's use isinstance() to confirm that the my_string variable is the string data type (str). We'll store the result in a variable called confirm:

my_string = "This is definitely a string"
confirm = isinstance(my_string, str)
print(confirm)

The result:

True

True is another data type called a boolean, along with it's counterpart False.

Python uses True and False almost like how we use Yes or No, Correct or Incorrect, Right or Wrong, Data or No data etc..

It's almost like we ask Python - Is my_string the str data type? To which Python has returned True!

We'll cover booleans in a future part of this series.

To illustrate using the isinstance function further, let's swap out str (string) for int (integer):

my_string = "This is definitely a string"
confirm = isinstance(my_string, int)
print(confirm)

The result:

False

We know my_string is definitely a string, so when we ask Python "Is my_string the int data type?", it returns False

The str() function

Sometimes we need to convert a different data type into a string.

For that, we can use another built in Python function str()

str() takes one value as an argument and will return a string of whatever value we pass it.

Let's create a new variable and assign it an integer value (a whole number) and convert it to a string, storing the result in a new variable:

digits = 22
string_digits = str(digits)
print(string_digits)

Result:

22

Ok so it's printed 22, but do we know it's a string?

Let's run string_digits through the isinstance() function to confirm we have a string!:

digits = 22
string_digits = str(digits)
confirm = isinstance(string_digits, str)
print(confirm)

The result:

True

Awesome, we've converted digits into a string and confirmed it using isinstance(string_digits, str).

String interpolation

Strings themselves are "immutable", meaning we can't just tell Python to add another character to the end of a string or delete the third character from the start of a string etc..

String interpolation, also known as variable substitution or variable expansion is the process of inserting variables or values into a string by creating "placeholders" and filling them in with something else!

Python has several forms of string interpolation which all achieve a similar result, however we're only going to cover 2 of the popular techniques.

F strings

F strings are the most recent form of string interpolation, introduced in 2015 to Python 3.6 and are my preferred way to format strings.

We create f strings by placing an f just before the opening quote of a string and create placeholders inside the string using sets of curly braces - {}.

We then fill in the curly braces with the variable or value we'd like to insert!

Here's an example. We'll create 2 new variables and insert them into a new f string:

subject = "Python"
adjective = "awesome"

message = f"Learning {subject} is {adjective}!"
print(message)

The result:

Learning Python is Awesome!

Python has evaluated the contents of the f string and rebuilt it with the contents of our placeholders.

We can also use f strings with multi line strings:

foo, bar, baz = "Foo", "Bar", "Baz"

multi = f"""
{foo}
{bar}
{baz}
"""
print(multi)

Prints:

Foo
Bar
Baz

There's lot's more interesting things we can do with f strings that are out of the scope of this guide. We'll cover them in more detail in a future part!

OK one more example..

We haven't covered math in this series, but heres an example of doing some math within an f string:

calc = f"9 to the power of 10 is {9**10}"
print(calc)

Returns:

9 to the power of 10 is 3486784401

f strings are a good choice as it's much easier to see how the string will look once it's been formatted.

Check the link at the bottom of the post to the Python documentation for more info on f strings.

String format

Another option to format strings is using the .format() method, which achieves the same result as f strings in a slightly different way.

To use it, we attach .format() to the end of a string containing placeholders ({}) and pass it arguments which it will then fill in.

Let's use the same example as above using .format():

subject = "Python"
adjective = "awesome"

message = "Learning {} is {}!".format(subject, adjective)
print(message)

Just like before, we get:

Learning Python is awesome!

format() will fill in the placeholders with arguments in the order we provide them.

Just like f strings, we can add logic to .format() which it will calculate and replace:

calc = "{} is the result of 100 divided by 25".format(100/25)
print(calc)

Returns:

4.0 is the result of 100 divided by 25

We can also adjust the ordering that the values passed to .format() are replaced, for example:

one = "First"
two = "Second"
three = "Third"

reordered = "{2}, {0}, {1}".format(one, two, three)

print(reordered)

The result:

Third, First, Second

.format() has replaced the placeholders with the 3 values, following the order we provided in the curly braces!

Tip - Python uses the base 10 numeric system, so the first value of anything is always 0

Just like f strings, there's lot's more we can do with .format(). Chack out the link at the bottom of the page for the documentation if you'd like to learn more.

String methods

Methods are similar to functions, however rather that pass a value into the function like print(value) or str(value), we call it on the object itself, just like "This is a {} string".format("cool"). You'll learn more about functions and methods later in this series

Python comes with some awesome built in methods that we can use on a string to transform it or return some information about the string.

Let's use a few of the string methods and print the result. We're going to use the interactive Python interpreter.

If you've not used the interactive Python interpreter before, the 3 arrows (>>>) indicate a command.

We'll use str to represent the string.

str.capitalize() - Returns a copy of the string with the first character capitalized:

"awesome".capitalize()

# 'Awesome' 

str.encode() - Returns a utf encoded copy of the string as bytes

"awesome".encode()

# b'awesome'

str.startswith(prefix) - Returns True if the string starts with prefix, otherwise False

"awesome".startswith("a")

# True

str.endswith(prefix) - Returns True if the string ends with prefix, otherwise False

"awesome".endswith("a")

# False

str.isalnum() - Returns True if all characters are alphanumeric, otherwise False

>>> "awesome".isalnum()
True
>>> "awe$ome".isalnum()
False

str.isalpha() - Returns True if all characters are alphabetic, otherwise False

>>> "abc".isalpha()
True
>>> "123".isalpha()
False

str.isdigit() - Returns True if all characters are digits, otherwise False

>>> "12.3".isdigit()
True
>>> "abc".isdigit()
False

str.isnumeric() - Returns True if all characters are numeric, otherwise False

>>> "123".isnumeric()
True
>>> "12.3".isnumeric()
False

str.lower() - Returns a lowercase copy of the string

>>> "HELLO WORLD".lower()
'hello world'

str.islower() - Returns True if all characters are lower case, otherwise False

>>> "hello world".islower()
True
>>> "HELLO WORLD".islower()
False

str.upper() - Returns an uppercase copy of the string

>>> "make me upper".upper()
'MAKE ME UPPER'

str.isupper() - Returns True if all characters are uppercase, otherwise False

>>> "PYTHON".isupper()
True
>>> "Python".isupper()
False

str.join(iterable) - Joins an iterable (such as a list, tuple or set)

>>> ", ".join(["Python", "is", "awesome"])
'Python, is, awesome'

str.strip(chars) - Returns a copy of the string with the leading and trailing characters removed

>>> "     spaced out     ".strip()
'spaced out'
>>> "\nstrip newline chars\n".strip("\n")
'strip newline chars'

str.lstrip(chars) - Returns a copy of the string with the leading characters removed

>>> "   spaced".lstrip()
'spaced'
>>> "this will be stripped".lstrip("this ")
'will be stripped'
>>>

str.rstrip(chars) - Returns a copy of the string with the trailing characters removed

>>> "space     ".rstrip()
'space'
>>> "please remove this".rstrip(" this")
'please remove'
>>>

str.partition(sep) - Returns a 3 part tuple containing the part before the separator, the part itself and the trailing part

>>> "abc".partition("b")
('a', 'b', 'c')
>>> "SplitMeUp".partition("Me")
('Split', 'Me', 'Up')

str.replace(x, y) Returns a string with x replaced with y

>>> "replace THIS".replace("THIS", "THAT")
'replace THAT'

str.split(sep=None, maxsplit=-1) - Returns a list of words in the string (sep = delimiter)

>>> "one two three".split()
['one', 'two', 'three']
>>> "foo+bar+baz".split("+")
['foo', 'bar', 'baz']

str.splitlines(keepends=False) - Returns a list of the lines in the string

>>> """multi\nline\nstring""".splitlines()
['multi', 'line', 'string']
>>> """multi\nline\nstring""".splitlines(True)
['multi\n', 'line\n', 'string']

str.swapcase() - Returns a copy of the string with the case swapped

>>> "wOaH cRaZy".swapcase()
'WoAh CrAzY'

str.title() - Returns a title cased copy of the string

>>> "HELLO WORLD".title()
'Hello World'

These are just a few of the common string methods which are extremely useful! We'll put a link at the end of this guide where you can read more about the string methods available in Python.

String prefixes

String prefixes allow us to change how Python evaluates a string.

You've already seen a string prefix in action with f strings, along with how it changes the way Python evaluates the string directly following it.

In addition to f strings, we also have access to the following prefixes.

b or B - Will produce a string of bytes:

bytestring = b"A string of bytes"

Strings in Python 3 are Unicode (utf-8) by default so we have to be explicit when creating a byte string.

r or R - Will produce a raw string:

raw_string = r"\n\btotally raw\a"

Unlike regular strings, raw strings ignore any escape characters.

If we were to print(raw_string) we would get the following output:

'\\n\\btotally raw\\a'

Wrapping up

Even through this was quite a long guide, we've only really covered the basics of working with strings in Python.

Strings are powerful, make up a huge portion of your code and fortunately for us, Python comes with lots of useful features for working with them, right out of the box.

Learning how to work with and manipulate strings will set you up for a great start when learning Python!

Further reading

Python strings: https://docs.python.org/3.7/library/string.html String formatting: https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals String methods: https://docs.python.org/3.7/library/stdtypes.html#string-methods

Last modified · 14 Mar 2019 Reference : https://pythonise.com/series/learning-python/python-strings

Last updated