Strings Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is: - String Concatenation?
- String Multiplication
- String length

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Indexing Strings
- Where does it start?
- What does it do?
- How to do it?

A
  • Starts at 0 not 1
  • Returns the single character
  • “string”[i]
  • i.e “Hello World”[6] = W
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does:
- Ord(“a”) do?
- Chr(97) do?
- Int(“1234”)
- Str(1234)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Format
I.e - “{0:<20}”.format(“one”)
- “{0:>20}”.format(“one”)
- “{0:^20}”.format(“one”)
- “{0:5.3f}”.format(1.23456789)

A
  • Left- aligned in fixed width
    “One “
  • Right-aligned in fixe width
    “ One”
  • Centered in fixed width
    “ One “
  • Floating point number rounding
    “1.235”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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]

A
  • “Jbewcy”
  • “ycwebJ”
  • “Jbe”
  • “rba”
  • “Jabbe”
  • “ykcow”
  • “rwocky”
  • “rebbaJ”
  • “rebba”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly