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:
Or double quotes:
However it's either one or the other:
The above example will cause a SyntaError
:
A string can contain any character (with a few workarounds which we'll cover shortly)
A string of numbers (Integer):
A string of decimal values (Float):
Even if a string contains numbers, it's still a string.
A string can contain numbers and letters:
Or non alphanumeric characters:
Or a mixture of the above:
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:
By including a single quote within our string, we're causing the string to be closed early and this will cause an error.
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:
Using a backslash \
just before the inner single quote to ESCAPE the inner quote:
Using triple single or triple double quotes to open and close the string:
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:
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:
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.
Escapes can be used for more than just dealing with inner quotes or backslashes, we can for example..
\n
- ASCII linefeed (New line):
Will print:
\a
- ASCII bell:
Will print an empty line and sound a bell!
\b
- ASCII backspace:
Returns:
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:
Is the same as:
And both will return:
Any spaces or tabs inside a multi line string will be preserved, for example:
Will print:
We can also use the backslash character to span a string over multiple lines, however it will be evaluated as a single line:
Prints:
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:
The result:
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:
The result:
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):
The result:
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:
Result:
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!:
The result:
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:
The result:
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:
Prints:
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:
Returns:
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()
:
Just like before, we get:
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:
Returns:
We can also adjust the ordering that the values passed to .format()
are replaced, for example:
The result:
.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:
str.encode()
- Returns a utf
encoded copy of the string as bytes
str.startswith(prefix)
- Returns True
if the string starts with prefix
, otherwise False
str.endswith(prefix)
- Returns True
if the string ends with prefix
, otherwise False
str.isalnum()
- Returns True
if all characters are alphanumeric, otherwise False
str.isalpha()
- Returns True
if all characters are alphabetic, otherwise False
str.isdigit()
- Returns True
if all characters are digits, otherwise False
str.isnumeric()
- Returns True
if all characters are numeric, otherwise False
str.lower()
- Returns a lowercase copy of the string
str.islower()
- Returns True
if all characters are lower case, otherwise False
str.upper()
- Returns an uppercase copy of the string
str.isupper()
- Returns True
if all characters are uppercase, otherwise False
str.join(iterable)
- Joins an iterable (such as a list, tuple or set)
str.strip(chars)
- Returns a copy of the string with the leading and trailing characters removed
str.lstrip(chars)
- Returns a copy of the string with the leading characters removed
str.rstrip(chars)
- Returns a copy of the string with the trailing characters removed
str.partition(sep)
- Returns a 3 part tuple containing the part before the separator, the part itself and the trailing part
str.replace(x, y)
Returns a string with x
replaced with y
str.split(sep=None, maxsplit=-1)
- Returns a list of words in the string (sep = delimiter)
str.splitlines(keepends=False)
- Returns a list of the lines in the string
str.swapcase()
- Returns a copy of the string with the case swapped
str.title()
- Returns a title cased copy of the string
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:
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:
Unlike regular strings, raw strings ignore any escape characters.
If we were to print(raw_string)
we would get the following output:
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