Python

Strings, Bytes & Formatting

51 flashcards · answers and spaced-repetition review in the KnowCard app

What does this print? ``` print(f"{3.14159:.2f}") ```
What does this print? ``` s = "hello" s.replace("l", "L") print(s) ```
Does calling s.upper() (or .strip(), .replace(), etc.) modify the string s in place?
What does this raise or print? ``` print("P" + b"ython") ```
What does this raise? ``` b = b"Püthøn" ```
You have str text and need to send it over a byte-oriented channel (network/file). Which direction is encode vs decode?
What does this raise? ``` b = "Püthøn".encode("utf-8") print(b.decode("ascii")) ```
What does this print? ``` name = "Sam" age = 30 print(f"{name} is {age}") ```
What does this print? ``` x = 42 print(f"{x=}") ```
What does this print (note the alignment spec)? ``` print(f"[{'hi':>10}]") ```
What does this evaluate to? ``` "name@host.com".strip("moc") ```
You want to drop a leading "www." from a URL string. Why is s.lstrip("www.") the wrong tool?
What's the difference between s.find(sub) and s.index(sub) when sub is absent?
What does this evaluate to? ``` "1---2".split("-") ```
What does this evaluate to? ``` "a.b.c".split(".", 1) ```
Why prefer "".join(parts) over building a string with += in a loop?
What does this raise? ``` ", ".join([1, 2, 3]) ```
What does this print? ``` path = "C:\new\text.txt" print(path) ```
What does r"..." (raw string) change about a string's value?
What do these evaluate to? ``` ord("A") chr(97) ```
What does this evaluate to? ``` "42".zfill(5) ```
You need to repeatedly modify individual bytes of a buffer. Why won't bytes work, and what does?
What does this evaluate to? ``` "hi {name}".format(naem="Sam") ```
What do these produce? bytearray(3) bytearray([72, 73])
What does this print? print(b"ABC"[0])
What does this raise, and how do you fix it? s = "First line second line"
What does this evaluate to? x = "First part" "Second part"
You need a string that contains an apostrophe, e.g. It's here. What's the simplest way to avoid a backslash escape?
What do these print, and why do they differ? a = [1, 2]; print(a[:] is a) s = "hi"; print(s[:] is s)
What does this evaluate to? "Unix\nWindows\r\nMac\rEnd".splitlines()
What does this evaluate to? "www.python-book.com".partition(".")
What does this evaluate to? "Mississippi".count("ss")
What does this evaluate to? "abcdef".replace("", "-")
For case-insensitive comparison of German text, why is casefold() safer than lower()?
What do these produce? s = "hello WORLD" s.capitalize() s.title()
What does this evaluate to? "hi".center(6, "*")
In str.format, what's the difference between {0}, {}, and {name} placeholders?
How do you output a literal { in a str.format template? "{{{0}}}".format(7)
What does this evaluate to? c = 3 + 4j "{0.real} / {0.imag}".format(c)
In a format spec, what does the = alignment do that > does not? "{:=8}".format(-42)
Set a custom fill character in a format spec: what does this print? "{:*^9}".format("hi")
What do these produce? "{:+d}".format(5) "{: d}".format(5) "{:-d}".format(5)
What does this print? print("{:#x} {:x} {:b}".format(255, 255, 5))
What does this print (note the type)? print("{:%}".format(0.75))
What do these produce? "{:,}".format(1234567) "{:_}".format(1234567)
What's the difference between the built-in format(value, spec) and str.format?
Three ways to embed the euro sign € in a string literal. What are \x, \u, and \N for?
What does this produce, and what would errors="strict" do instead? "café".encode("ascii", "ignore")
What character encoding does Python 3 assume your source .py file uses, and how do you override it?
What do the escape sequences \a, \b, and \r stand for?
What does each of these produce? str(b"hi") str(b"hi", "utf-8")

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play