This post is a record made while learning Chapter 2 “Variables and Simple Data Types” in Eric Matthes’s book, Python Crash Course.
String
Create a string
A string is a series of characters, and anything inside quotes (single or double quotes) is considered a string in Python:
1
| "This is a string.", 'This is also a string.'
|
1
| ('This is a string.', 'This is also a string.')
|
This flexibility make it is easy to use quotes and apostrophes in a string:
1
| 'I told my friend, "Python is my favorite language!"', "The language 'Python' is named after Monty Python, not the snake.", "One of Python's strengths is its diverse and supportive community."
|
1
2
3
| ('I told my friend, "Python is my favorite language!"',
"The language 'Python' is named after Monty Python, not the snake.",
"One of Python's strengths is its diverse and supportive community.")
|
Change case in a string: title()
, upper()
, and lower()
title()
method changes each word to title case, namely the first letter of each word uppercase and other letters lowercase.
1
2
| name = "aDA loVeLAce"
print(name.title())
|
1
2
| print("Ada".title(), "ADA".title(), "ada".title())
print("Ada".title() == "ADA".title(), "ADA".title() == "ada".title())
|
1
2
| Ada Ada Ada
True True
|
Similarly, we can change a string to all uppercase by upper()
method, and all lowercase by lower()
method:
1
2
3
| name = "aDA loVeLAce"
print(name.upper())
print(name.lower())
|
1
2
| ADA LOVELACE
ada lovelace
|
On the other hand, we should note that title()
, upper()
, and lower()
methods won’t change the value that was originally stored in name
:
1
2
3
4
5
6
7
8
9
10
11
| name = "aDA loVeLAce"
name.title()
print(name)
name = "aDA loVeLAce"
name.upper()
print(name)
name = "aDA loVeLAce"
name.lower()
print(name)
|
1
2
3
| aDA loVeLAce
aDA loVeLAce
aDA loVeLAce
|
Use variables in a string (f-string)
To insert a variable’s value into a string, place the letter “f” immediately before the opening quotation mark, and put braces around the name or names of any variable to be inserted:
1
2
3
4
5
| first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)
print(f"Hello, {full_name.title()}!")
|
1
2
| ada lovelace
Hello, Ada Lovelace!
|
These strings are called f-strings. The “f” is for format, because Python formats the string by replacing the name of any variable in braces with its value.
F-strings were first introduced in Python 3.6. In an earlier-version Python, we need to use the format()
method rather than f-strings:
1
2
| full_name = "{} {}".format(first_name, last_name)
print(full_name)
|
Add whitespace (tabs or newlines) to a string: \t
and \n
In programming, whitespace refers to any nonprinting character, such as spaces, tabs, and end-of-line symbols.
To add a tab in a string, use the character combination \t
; to add a newline, use the character combination \n
:
1
| print("Languages:\n\tPython\n\tC\n\tJavaScript")
|
1
2
3
4
| Languages:
Python
C
JavaScript
|
Strip whitespace from a string: rstrip()
, lstrip()
, and strip()
Strip the extra whitespace on the right side of a string by rstrip()
method, left ones by lstrip()
, and both side by strip()
:
1
2
| favorite_language = ' python '
favorite_language.rstrip(), favorite_language.lstrip(), favorite_language.strip()
|
1
| (' python', 'python ', 'python')
|
However, it should be noted that the whitespace is only removed from the string temporarily. To make whitespace removed permanently, we should associate the stripped value with the variable name, like:
1
2
3
| favorite_language = 'python '
favorite_language = favorite_language.rstrip()
favorite_language
|
In the real world, these stripping functions are often used to clean up user input before it’s stored in a program.
Number
Integer
In Python, two multiplication symbols are used to represent exponents:
1
| 3 ** 2, 3 ** 3, 10 ** 6
|
Float
Python calls any number with a decimal point a float. This term is used in most programming languages, and it refers to the fact that a decimal point can appear at any position in a number.
1
| 0.1 + 0.1, 0.2 + 0.2, 2 * 0.1, 2* 0.2
|
But sometimes we’ll get an arbitrary number of decimal places:
1
| (0.2 + 0.1), (3 * 0.1), (0.2 + 0.1 == 0.3), (3 * 0.1 == 0.3), (0.2 + 0.1) == (3 * 0.1)
|
1
| (0.30000000000000004, 0.30000000000000004, False, False, True)
|
This problem has to do with floating point representation, and happens in all languages and is usually of little concern. A remedy to it is using native Python decimal
module:
1
2
3
4
| from decimal import *
a, b, c = Decimal('0.1'), Decimal('0.2'), Decimal('0.3')
a+b == c
|
Mix integer and float in an operation
Python defaults to a float in any operation that uses a float, even if the output is a whole number (integer):
1
| 4/2, 1 + 2.0, 2 * 3.0, 3.0 ** 2
|
Underscores in number
Group digits using underscores to make large numbers more readable:
1
2
3
4
5
6
7
| universe_age = 14_000_000_000
universe_age1 = 14_000000000
print(universe_age, universe_age1)
print(universe_age == universe_age1)
universe_age2 = 1_4000000000.12_342
print(universe_age2)
|
1
2
3
| 14000000000 14000000000
True
14000000000.12342
|
As can be see, this feature works for both integer and float, but only available in Python 3.6 and later.
Constant
A constant is like a variable whose value stays the same throughout the life of a program. Python doesn’t have built-in constant types, but Python programmers commonly use all capital letters to indicate a variable should be treated as a constant and never be changed:
1
2
| MAX_CONNECTIONS = 5000
print(MAX_CONNECTIONS)
|
Make a long number readable
Method 1: by exponents
Method 2: by inserting underscores
Method 3 (preferable): by scientific notation
1
2
3
4
5
6
| print(1.234e10)
print(1.234E10)
print(float("1.234e10"))
print(float("1.234E10"))
print(float('1.234e10'))
print(float('1.234E10'))
|
1
2
3
4
5
6
| 12340000000.0
12340000000.0
12340000000.0
12340000000.0
12340000000.0
12340000000.0
|
References