Python : Intro to strings Flashcards
In Python, the way we store something like a word, a sentence, or even a whole paragraph is as a ?
String
What would be the code if you wanted to save your favorite word as a string to the variable favorite_word ?
favorite_word = “bella”
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?
favorite_fruit[0]
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?
my_name = “Anthony”
first_initial = my_name[0]
What is it called when we select entire chunks of characters from a string?
slicing
the syntax below is an example of what?
string_name[first_index:last_index]
slicing syntax
If the first index is removed from the slicing syntax, the slice starts at the ?
the slice starts at the beginning
If the second index is removed from the slicing syntax, the slice does what?
Continues to the end of the string
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.
first_name = "Julie" last_name = "Blevins"
def account_generator(first_name, last_name): return first_name[0:3] + last_name[0:3]
What does the len() function do?
len() returns the number of characters in a string
The len() function returns what?
the number of characters in a string
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.
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
Negative indices count backward from the end of the string, so string_name[-1] is the ?? of the string
last character of the string
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
final_word = company_motto[-4:]
Strings are ???. Which means that we cannot change a string once it is created.
immutable