Python : Intro to strings Flashcards

1
Q

In Python, the way we store something like a word, a sentence, or even a whole paragraph is as a ?

A

String

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

What would be the code if you wanted to save your favorite word as a string to the variable favorite_word ?

A

favorite_word = “bella”

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

Consider the string
favorite_fruit = “blueberry”. If we wanted to select the first letter from this string using the index. What would be the code?

A

favorite_fruit[0]

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

Save your name as a string to the variable my_name. Then select the first letter of the variable my_name and save it to first_initial. What would the code look like?

A

my_name = “Anthony”

first_initial = my_name[0]

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

What is it called when we select entire chunks of characters from a string?

A

slicing

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

the syntax below is an example of what?

string_name[first_index:last_index]

A

slicing syntax

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

If the first index is removed from the slicing syntax, the slice starts at the ?

A

the slice starts at the beginning

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

If the second index is removed from the slicing syntax, the slice does what?

A

Continues to the end of the string

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

Write a function called account_generator that takes two inputs, first_name and last_name and concatenates the first three letters of each and then returns the new account name. Use the variables above.

A
first_name = "Julie"
last_name = "Blevins"
def account_generator(first_name, last_name):
  return first_name[0:3] + last_name[0:3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the len() function do?

A

len() returns the number of characters in a string

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

The len() function returns what?

A

the number of characters in a string

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

Write a function called password_generator that takes two inputs, first_name and last_name and then concatenate the last three letters of each and returns them as a string.

A
def password_generator(first_name, last_name):
  last_three_first = first_name[len(first_name)-3:]
  last_three_last = last_name[len(last_name)-3:]
  return last_three_first + last_three_last
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Negative indices count backward from the end of the string, so string_name[-1] is the ?? of the string

A

last character of the string

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

There is a variable named company_motto. How woudl you use negative indices to create a slice of the last 4 characters in company_motto

A

final_word = company_motto[-4:]

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

Strings are ???. Which means that we cannot change a string once it is created.

A

immutable

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

Write a new function called get_length() that takes a string as an input and returns the number of characters in that string. Do this by iterating through the string, don’t cheat and use len()!

A
def get_length(word):
  counter = 0
  for letter in word:
    counter += 1
  return counter
17
Q

Escape sequences are used to indicate what?

A

That we want to split something in a string that is not necessarily a character

18
Q

Newline or \n will allow us to split a multi-line string by what?

A

By line breaks

19
Q

\t will allow us to split a string by what?

A

By tabs