Casting Examples
>>> int(4.5)
4
>>> int(-4.5)
-4
>>> int(1.5e100)
14999999999999999267208920533534235242810259434158468763468571810551997532349366721768062376180973568
>>> int(1.5555555555e100)
15555555555000000634775410563936071849876978208250285056660387478653424091320529666325108209909497856
>>> bool(2.3)
True
>>> bool(0)
False
>>> int("cows")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'cows'
>>> str(True)
'True'
>>> str(23)
'23'
>>> str(1.5e6)
'1500000.0'
>>> x = 3.4
>>> int(x)
3
Format Strings These are preceded by an f
.
Expressions inside of curly braces are evaluated and droppped into the string.
To make an actual curly brace, use \{
.
Special Characters These are preceded by a \
.
>>> print("quack\tmooo\tbaaaa") #\t is tab
quack mooo baaaa
>>> print("1\n2\n3\n") #\n is newline
1
2
3
You can get your favorite unicode characters using \uXXXX
, where
each X
represents a hex digit.
>>> print("\u03b1")
α
>>> print("\u03b2")
β
>>> print("\u03b3")
γ
>>> print("\u03b4")
δ
>>>
Raw StringsThese are preceded by an r
. Within them,
a \ is just a \. They turn off characters specialness.
>>> x = r"C\Crap\BillGates\sux"
>>> print(x)
C\Crap\BillGates\sux
>>> x = r"C\Crap\BillGates\sux\n\t"
>>> print(x)
C\Crap\BillGates\sux\n\t
>>> x = r"C:\Crap\BillGates\sux\n\t"
>>> x
'C:\\Crap\\BillGates\\sux\\n\\t'
>>> f"""{x} is the root cause
... of Microsoft's crappiness"""
"C:\\Crap\\BillGates\\sux\\n\\t is the root cause\nof Microsoft's crappiness"
>>> y = f"""{x} is the root cause
... ... of Microsoft's crappiness"""
>>> print(y)
C:\Crap\BillGates\sux\n\t is the root cause
... of Microsoft's crappiness
Triple-Quoted Strings These can span several lines.
>>> x = """This
... goes on
... and on
... and on
... and on"""
>>> print(x)
This
goes on
and on
and on
and on
Single quotes can be used for triple quoted string; you must use the same type of quote throughout.
String Methods
Remember, strings are immutable, so any string method actually returns a modified copy of a string. The original string is untouched.
Capitalize a string.
>>> x = "foo"
>>> x.capitalize()
'Foo'
>>> x
'foo'
Be the center of attention.
>>> x.center(40)
' foo '
>>> x.center(40, "*")
'******************foo*******************'
Note that the second optional argument can only be one character.
>>> x.center(40, "bar")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: The fill character must be exactly one character long
Time to visit count chocula.
>>> state = "mississippi"
>>> state.count("m")
1
>>> state.count("s")
4
>>> state.count("i")
4
>>> state.count("is")
2
You can say where to start or where to start or end. These arguments are optional.
>>> state.count("is", 5)
0
>>> state.count("is", 1, 5)
1
It's all right in the end.
>>> state.endswith("i")
True
>>> state.endswith("pi")
True
>>> state.endswith("ippi")
True
Here is how to find from the start.
>>> state.find("p")
8
>>> state
'mississippi'
Here is how to find from the end.
>>> state.rfind("p")
9
Goose Chase!
>>> state.rfind("quack")
-1
Verify a string contains only alpha characeters.
>>> state.isalnum()
True
>>> foo = "cats;dogs"
>>> foo.isalnum()
False
Digits...
>>> x = "2344"
>>> x.isdigit()
True
>>> x = "23 44"
>>>
>>> x.isdigit()
False
Whitespace (\t, \n, \r (space))
>>> "\t".isspace()
True
Strip!
>>> x = " space cadet "
>>> x.strip()
'space cadet'
>>> x = " space cadet \t\t\n "
>>> x.strip()
'space cadet'
>>> x = " space cadet \t\t\n "
>>> x.strip()
'space cadet'
Strip the left or right.
>>> x.lstrip()
'space cadet \t\t\n '
>>> x.rstrip()
' space cadet'
Replace. In with the new, out with the old.
>>> x = "XXX is prohibited on the premises. You can't have XXX"
>>> x
"XXX is prohibited on the premises. You can't have XXX"
>>> x.replace("XXX", "Likker")
Only replace one.
"Likker is prohibited on the premises. You can't have Likker"
>>> x.replace("XXX", "Likker", 1)
"Likker is prohibited on the premises. You can't have XXX"
>>> x = 4
>>> y = 13
>>> print(f"{x}*{y} = {x*y}")
4*13 = 52
Make us!
* ** *** **** *****
Here are some solutions made by the class.
Solution 1, triple-quoted simplicity:
print("""
*
**
***
****
*****""")
Solution 2 \n games:
print("*\n***\n***\n****\n*****")
Solution 3, fashion-forward format foolery:
x = "*"
print(f"*\n{x*2}\n{x*3}\n{x*4}\n{x*5}")
Try these for next time!
* *** ***** ******* *********
* * * * * * * * * * * * * * * * * * * * * * * * *
Using input