Python String
Python String
Summary: in this tutorial, you’ll learn about Python string and its basic operations.
If a string contains a single quote, you should place it in double-quotes like this:
And when a string contains double quotes, you can use the single quotes:
To escape the quotes, you use the backslash (\). For example:
The Python interpreter will treat the backslash character (\) special. If you don’t want it to do so,
you can use raw strings by adding the letter r before the first quote. For example:
message = r'C:\python\bin'
To span a string multiple lines, you use triple-quotes “””…””” or ”’…”’. For example:
help_message = '''
Usage: mysql command
-h hostname
-d database name
-u username
-p password
'''
print(help_message)
For example, you may want to use the value of the name variable inside the message string
variable:
name = 'John'
message = 'Hi'
To do it, you place the letter f before the opening quotation mark and put the brace around the
variable name:
name = 'John'
message = f'Hi {name}'
print(message)
Python will replace the {name} by the value of the name variable. The code will show the
following on the screen:
Hi John
The message is a format string, or f-string in short. Python introduced the f-string in version 3.6.
Output:
Good Morning!
Output:
Good Afternoon!
How it works:
● Then, access the first and second characters of the string by using the square brackets
[] and indexes.
If you use a negative index, Python returns the character starting from the end of the string. For
example:
+---+---+---+---+---+---+---+---+---+---+---+---+---+
| P | y | t | h | o | n | | S | t | r | i | n | g |
+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8 9 10 11 12
-13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Output:
13
Slicing strings
Slicing allows you to get a substring from a string. For example:
Output:
Py
The str[0:2] returns a substring that includes the character from the index 0 (included) to 2
(excluded).
string[start:end]
The substring always includes the character at the start and excludes the string at the end.
The start and end are optional. If you omit the start, it defaults to zero. If you omit the end, it
defaults to the string’s length.
Error:
Traceback (most recent call last):
File "app.py", line 2, in <module>
str[0] = 'J'
TypeError: 'str' object does not support item assignment</module>
When want to modify a string, you need to create a new one from the existing string. For example:
Output:
Jython String
Summary
● Use quotes, either single quotes or double quotes to create string literals.
● Place literal strings next to each other to concatenate them. And use the + operator to
concatenate string variables.
● Use the str[n] to access the character at the position n of the string str.
Python Boolean
Summary: in this tutorial, you’ll learn about the Python boolean data type, falsy and truthy values.
To represent true and false, Python provides you with the boolean data type. The boolean value
has a technical name as bool.
The boolean data type has two values: True and False.
Note that the boolean values True and False start with the capital letters (T) and (F).
is_active = True
is_admin = False
When you compare two numbers, Python returns the result as a boolean value. For example:
>>> 20 > 10
True
>>> 20 < 10
False
>>> bool('Hi')
True
>>> bool('')
False
>>> bool(100)
True
>>> bool(0)
False
As you can see clearly from the output, some values evaluate to True and the others evaluate to
False.
● False
● None
● An empty list []
● An empty tuple ()
● An empty dictionary {}
The truthy values are the other values that aren’t falsy.
Note that you’ll learn more about the None, list, tuple, and dictionary in the upcoming
tutorials.
Summary
● Python boolean data type has two values: True and False.
● The falsy values evaluate to False while the truthy values evaluate to True.
● Falsy values are the number zero, an empty string, False, None, an empty list, an
empty tuple, and an empty dictionary. Truthy values are the values that are not falsy.