Strings Flashcards
What is: - String Concatenation?
- String Multiplication
- String length
Processing strings
- Joins strings i.e “Hello” + “ “ + ”World”
- Multiplies a string i.e “Hello”*3 = HelloHelloHello
- Finds the length of a string I.e len(“ciao) = 4
Indexing Strings
- Where does it start?
- What does it do?
- How to do it?
- Starts at 0 not 1
- Returns the single character
- “string”[i]
- i.e “Hello World”[6] = W
What does:
- Ord(“a”) do?
- Chr(97) do?
- Int(“1234”)
- Str(1234)
- Returns the Unicode number for 1-character string a
- Returns 1-character string with Unicode symbol 97
- returns integer value 1234
- Returns the string value 1234
Slicing
What is the syntax?
Where does the backward indexing start?
How to slice from back to front?
Are the parameters optional?
What is essential?
- Syntax: Variable[Start:Stop:Step]
- Starts at -1
- If Step is negative
- Start is optional, assumed as 0 if no step. -1 if step is negative
Stop is optimal, assumed to be Len, -Len-1 if step is negative
Step is optional, assumed as 1 - At least one colon must be included - even if all parameters are left out
String Functions
- Syntax
- How to look for the integer number?
- How to teeny position of first string y in x?
- How to return lower case?
- How to return upper case?
- How to return a version of string_x with every occurrence of string y replaced with new?
Syntax - string_x.function(string_y)
- Count — string_x.count(string_y)
- Find — string_x.find(string_y)
- Lower — Bro.lower()
- Upper — Bro.upper()
- Replace — String_x.replace(string_y, new)
Format
I.e - “{0:<20}”.format(“one”)
- “{0:>20}”.format(“one”)
- “{0:^20}”.format(“one”)
- “{0:5.3f}”.format(1.23456789)
- Left- aligned in fixed width
“One “ - Right-aligned in fixe width
“ One” - Centered in fixed width
“ One “ - Floating point number rounding
“1.235”
Examples. What do these produce?
a = “Jabberwocky”
— b = a[::2]
— c = a[::-2]
— d = a[0:5:2]
— e = a[5:0:-2]
— f = a[:5:1]
— g = a[:5:-1]
— h = a[5::1]
— i = a[5::-1]
— j = a[5:0:-1]
- “Jbewcy”
- “ycwebJ”
- “Jbe”
- “rba”
- “Jabbe”
- “ykcow”
- “rwocky”
- “rebbaJ”
- “rebba”