Strings
So far, we have already stored some text in a variable. For example
"Hello World!"
which is called a string. A string is a primitive data
type. Integer, float, boolean and None are also primitive data types which we
will cover later.
A string is simply a series of characters. Anything inside quotes is considered
a string in Python
, and you can use single ('
) or double
quotes ("
)
around your strings like this:
This flexibility allows you to use quotes and apostrophes within your strings:
type()
Let's check the type of the variable text
.
type()
comes in handy to check the type of variables. In this
case, we can verify that text
is indeed a string. Just like
print()
, type()
is an important tool in your programming arsenal.
Info
It is advisable to consistently enclose your strings with either single
'...'
or double quotes "..."
. This will make your
code easier to read and maintain.
String methods
One of the simplest string manipulation, is to change the case of the words in a string.
A method is an action performed on an object (in our case the
variable). The dot (.) in name.title()
tells Python
to
make the title()
method act on the variable name
which holds
the value "paul atreides"
.
Every method is followed by a set of parentheses, because methods often need
additional information to do their work. That information is
provided inside the parentheses. The title()
method doesn’t need
any additional information, so its parentheses are empty.
Methods vs. functions
We have already encountered functions like print()
and type()
. Functions are standalone entities that perform a specific task.
On the other hand, methods are associated with objects. In this case, the
title()
method is associated with the string object name
.
String methods
You start with the variable input_string
that holds the value
"fEyD rAuThA"
.
Experiment and apply a combination of the following methods:
capitalize()
title()
istitle()
isupper()
upper()
lower()
Eventually you should end up with the string "Feyd Rautha"
,
print it.
Concatenation
It’s often useful to combine strings. For example, you might want to store first and last name in separate variables, and then combine them when you want to display someone’s full name.
Python uses the plus symbol (+
) to combine strings. In this
example, we use +
to create a full name by combining a
first_name
, a space, and a last_name
:
first_name = "paul"
last_name = "atreides"
full_name = first_name + " " + last_name
print(full_name)
Here, the full name is used in a sentence that greets the user, and
the title()
method is used to format the name appropriately. This code
returns a simple but nicely formatted greeting:
Another way to nicely format strings is by using f-strings. To achieve the same
result as above, simply put an f"..."
in front of the string and use
curly braces {}
to insert the variables.
full_name = "Alia Atreides"
print(f"Hello, {full_name}!")
# you can even apply methods directly to the variables
# (within the curly braces)
print(f"Hello, {full_name.lower()}!")
A quote
Find a quote from a famous person you admire. Store the quote and name in variables and print both with an f-string.
Your output should look something like the following, including the quotation marks:
Frank Herbert (Dune opening quote): "I must not fear. Fear is the
mind-killer."
Recap
This section was all about strings, we have covered:
- How to create strings
- Use
type()
to check a variable's type - String methods, such as
title()
andlower()
- Distinction between methods and functions
- String concatenation with
+
and f-strings (f"..."
)
Next, up we will introduce numbers in Python, namely integers and floats.