Now we will learn about strings. You already know how to write them in Python code.
'This is string'
"And this is also string"
Sometimes you will need a string that is multiple lines long. But you can write strings only on one line in Python (you can actually write on more lines but the text would appear on just one).
In your text, you can use a special character that means
new line \n
:
print('Hello word\nHow are you?')
A backslash allows us to write characters which we can't easily write on the keyboard.
The backslash also allows us to use both types of quotes in one piece of text.
print('"Don\'t do it", said dad.')
print("\"Don't do it\", said dad.")
Backward slashes can also insert exotic characters
that you do not have on the keyboard.
Fancy characters can be written as \N
and a character
name in compound ("curly") braces.
Try for example the following characters
(some might not work for your system):
print('--\N{LATIN SMALL LETTER L WITH STROKE}--')
print('--\N{SECTION SIGN}--')
print('--\N{PER MILLE SIGN}--')
print('--\N{BLACK STAR}--')
print('--\N{SNOWMAN}--')
print('--\N{KATAKANA LETTER TU}--')
If you want to write a backslash character in your text
you have to write it twice (for example, in a path to a
file in Windows).
So the sequence \\
means one backslash.
print('C:\\PyLadies\\New Folder')
But back to multi-line strings. There is also another way how to write them in Python. You just have to wrap them in three single or three double quotes:
basen = '''Hello World!
How are you?'''
Programmers also use three quotes to document their functions.
It is called a docstring
(documentation string).
You do not need to worry about how to define a function, the next lecture will be aimed directly at this topic.
def multiply(a, b):
""" This function multiplies two arguments and returns the result.
Both arguments should be numbers.
"""
return a * b
However you can use the inbuilt help()
function on any function to see its "docstring"
.
In order to leave the newly opened help window, press q
as quit
.
Now we will have a look at how to work with strings.
You already know how to concatenate strings by addition and multiplication.
concatenated_string = 'a' + 'b'
long_string = 'o' * 100
Now we will learn how we can get part of a string. We will start with single characters. This is done by subscripting. The Syntax looks similar to calling a function but with square brackets!
fifth_character = 'PyLadies'[5]
print(fifth_character)
Does it work? Did you really get the fifth character?
As you may have already noticed, programmers in Python start counting from zero. First comes 0, then 1, and so on.
It's the same with strings - the first character is on position zero.
Why is it like that? You would have to know about pointers and arrays to fully understand, so now let's just assume that programmers are weird. Or that they just like weird numbers.
[0] [1] [2] [3] [4] [5] [6] [7]
╭───┬───┬───┬───┬───┬───┬───┬───╮
│ P │ y │ L │ a │ d │ i │ e │ s │
╰───┴───┴───┴───┴───┴───┴───┴───╯
What happens if you pick characters with negative numbers?
Strings can do more tricks. You can find out how long the string is, or if it contains a certain substring.
Code | Description | Example |
---|---|---|
len(r) |
Length of string | len('PyLadies') |
x in r |
True if the string x is in the string r |
'Ladies' in 'PyLadies' |
x not in r |
The opposite of x in r |
'eye' not in 'PyLadies' |
Python is case sensitive, so 'ladies' in 'PyLadies'
is False
. If you want to do a case insensitive test,
you would have to change the case of both strings
(both to lower, or both to upper) and then do x in y
.
And how to change the case of our string? To do that, we will need another Python feature: methods.
Methods are like functions - we can call something with them.
Unlike a function, a method is tied to some object.
It is called by writing a dot
and a method name just after the object.
And then, of course, parentheses, which may contain arguments.
The String methods upper()
and lower()
change the case.
string = 'Hello'
print(string.upper())
print(string.lower())
print(string)
Notice that the original string has not changed. Methods return a new string and the old string stays as it was.
That is Python's standard behavior: already existing string can't be changed, we can only create a new one - derived from the old one.
But it is not true that all methods do not change the original object they are being called on. This is true just for strings.
For practicing methods and subscripting, try to write a program, which will ask the user for their name, then their surname and then it will print their initials - the first letter of name and surname.
Initials are always upper case (even if the user won't write it that way).
There are many more string methods. You can find the most useful ones in our cheatsheet.
All methods are in the Python documentation.
Notice that len
isn't a method but a function. It's
written len(s)
not r.len()
.
You will find out why it is like that in a minute.
Especially useful is the format
method, which replaces
a pair of curly braces in string for whatever it
gets as an argument.
write = '{}×{} equals {}'.format(3, 4, 3 * 4)
print(write)
The String '{}×{} equals {}'
is something like a template.
Imagine it as form, where we have highlighted fields where Python
fills in values.
If you want to fill values in a different order, or you want the template to be clearer, you can write variables into your curly braces:
write = 'Hi {name}! The result is {number}.'.format(number=7, name='Mary')
print(write)
Formatting is used when you need to include a variable value in the string.
number = 3 + 4
name = "Mary"
write = 'Hi {}! The result is {}.'.format(name, number)
print(write)
There is another (newer and shorter) way how to use formatting in Python and it involves f-strings
.
Instead of using .format() method, you prepend letter f
before
the string or the multiline string and Python will replace the variable names present
in curly brackets inside the template with their values during script execution.
The syntax looks like this:
number = 3 + 4
name = "Mary"
write = f"Hi {name}! The result is {number}."
print(write)
Now we will go back to subscripting. Try to find out what the following program does:
string = 'PyLadies'
substring = string[5:]
print(substring)
Keep in mind that we are still counting from 0!
We can also use string[:5]
, which will select all characters
up to the fifth character, which is not included.
So string[:5] + string[5:] == string
.
What does string[2:5]
do?
And what about string[-4:]
?
string = 'PyLadies'
print(string[:4])
print(string[2:5])
print(string[-4:])
You have to think about which number, which index, you want to use.
It is better to think of these numbers as being on the borderlines between characters, it makes it easier to understand:
╭───┬───┬───┬───┬───┬───┬───┬───╮
│ P │ y │ L │ a │ d │ i │ e │ s │
├───┼───┼───┼───┼───┼───┼───┼───┤
│ │ │ │ │ │ │ │ │
0 1 2 3 4 5 6 7 8
-8 -7 -6 -5 -4 -3 -2 -1
╰───────────────╯
'PyLadies'[:4] == 'PyLa'
╰───────────────╯
'PyLadies'[2:6] == 'Ladi'
╰───────────╯
'PyLadies'[-3:] == 'ies'