Strings Flashcards

1
Q

Stings

A

Strings are ordered sequences of characters, using the syntax of either single quotes or double quotes:

  • ‘hello’
  • “hello”
  • ” I don’t do that “
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Indexing

A

Grabbing a single character from a string [use brackets]. Python indexing starts at 0.
h e l l o
0 1 2 3 4

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

Reverse Indexing

A

Same as indexing but backwards
h e l l o
0 1 2 3 4
0 -4 -3 -2 -1

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

Slicing

A

Allows you grab a subsection of multiple characters
[start:stop:step]
Start - a numerical index from the slice start
Stop - the index you will go up to (but not include)
Step - the size of the “jump” you take

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

” “

A

Denotes a string. Used for a string with a contraction word i.e. “I don’t think so”

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

Escape Sequence

A

\n = new line
print(‘hello \n world’)
hello
world

\t = tab space
print(‘hello \tworld’)
hello world

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

Length Function Len()

A

Len(‘hello’)
5
Len(‘I am)
4

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

Indexing

A

mystring = “Hello World”
mystring[0]
H

mystring[2:] #from this character onward
mystring[:3] #go up to that position, but do not include that character

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

Slicing

A

[start:stop:step]
mystring[3:6]
mystring[::-1] #Easy trick to reverse a string!!

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

Slicing is Immutable. True or False?

A

True

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

Slicing is immutable. What does that mean?

A

Once a string is defined it cannot be sliced to redefine part of it.

name = "Sam"
name[0] = 'P'    #This will give an error because 'str' object does not support item assignment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

String Concatination

A

Adding strings together

x = ‘Hello World’
x + “ it is beautiful outside!”
‘Hello World it is beautiful outside!

letter = ‘z’
letter * 10
‘zzzzzzzzzz’

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

You can concatinate a number and a string? True or False?

A

False

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

Strings are mutable. True or False?

A

False

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

How do you create comments in your code?

A

#

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

String Interpolation

A

Fancy way of saying: injecting variables into a string.

my_name = “Jose”
print(“Hello” + my_name)

17
Q

.format method

A

Method for doing string interpolation

print(“The result was {r:1.5f}”.format(r = result))
The result was 0.12870

18
Q

Float Formatting

A

“{value:width.precision f}”
r : 1 . 5 f
print(“The result was {r:1.5f}”.format(r = result))
The result was 0.12870

19
Q

f string method (aka formatted strings literal)

A

Quick way of doing string interpolation.

name = “Jose”
age = “3”
print(f’{name} is {age} years old.’)
Jose is 3 years old.