Strings Flashcards

1
Q

Use a for loop with an integer counter to print the following ten lines:

1 little Indians
2 little Indians

10 little Indians

A

for counter in range(1, 11):
print(str(counter) + “ little Indians”)

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

Join all path parts with backslash signs (\) and print the entire string.

path_parts = [‘c:’,’users’,’john’,’images’,’camera’,’edited’,’001.img’]

A

print(‘\'.join(path_parts))

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

Use ‘join’ to print the countries “I have visited”:

visited_countries = [‘Norway’, ‘Sweden’, ‘Denmark’, ‘Germany’, ‘France’]

A

visited_countries = [‘Norway’, ‘Sweden’, ‘Denmark’, ‘Germany’, ‘France’]
separator = ‘, ‘
print(‘I have visited the following countries: ‘ + separator.join(visited_countries))

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

Write code that prints your name and your age.

A

user_name = input(“Enter your name: “)

user_age = int(input(“Enter your age: “))

greeting = user_name + ‘, you are ‘ + str(user_age) + ‘!’

print(greeting)

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

Print email address. If not a valid email address, print “Not a correct email address”.

A

user_input = input(‘Enter your email address: ‘)
if ‘@’ not in user_input:
print(‘Not a correct email address!’)
else:
print(‘Thank you’)

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

Ask the user to do the following:

Provide the name of a country that does not contain any lowercase a or e letters. (Write the following prompt: ‘The country is: ‘)
If the user provides a correct string (i.e., one with no a or e inside it), print: You won… unless you made this name up! Otherwise, print: You lost!

A

country = input(‘The country is: ‘)
if ‘a’ in country or ‘e’ in country:
print(‘You lost!’)
else:
print(‘You won… unless you made this name up!’)

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

Write code that checks every single character in the string, where you register a username without digits. If any character is a digit, a warning is shown.

A

username = input(‘To register, enter a username without digits: ‘)
for character in username:
if character.isdigit():
print(character + ‘ is a digit!’)

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

Count and print how many times the letter ‘e’ appears in a poem.

A

def count_letter_e(poem):
count = 0
for letter in poem:
if letter.lower() == ‘e’:
count += 1
return count

poem = input(“Enter the poem: “)
letter_e_count = count_letter_e(poem)
print(“The letter ‘e’ appears”, letter_e_count, “times in the poem.”)

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

Ask the user to provide a question (i.e., a string with a question mark at the end). Write: Please provide a question in English: If the question is correct, print: Correct question. Otherwise print: Incorrect question. Use a negative string index!

A

user_question = input(‘Please provide a question in English: ‘)
if user_question[-1] == ‘?’:
print(‘Correct question’)
else:
print(‘Incorrect question’)

OR

question = input(“Enter a question here: “)
if “?” not in question:
print(“Incorrect!”)
else:
print(“Correct!”)

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

Here is pi with 75 digits after the decimal point:

3.141592653589793238462643383279502884197169399375105820974944592307816406286

Use string slicing to print the first 10 digits after the decimal point.

A

print(pi[2:12])

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

Print each log in the logs list on a new line, and don’t include the time part.

logs = [
‘12:04 User logged in.’,
‘12:07 User tried to access a restricted resource.’,
‘12:09 User sent a message to the admin.’,
‘12:10 User logged out.’
]

A

for log in logs:
print(log[6:])

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

A secret message was encoded in the string secret_message. To decode the message, print every other character, beginning at the character with the index 1.

secret_message = ‘xVderrfyt jwkehlll4,y oyuo2us 5bqrto3kteh et hfea bcno3djek!’

A

print(secret_message[1::2])

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

Ask the user to provide a palindrome, a word that reads the same backwards and forwards: Write a palindrome:

If the input is an actual palindrome, write: Correct palindrome. Otherwise write: Not a palindrome.

A

if palindrome == palindrome[::-1]:
print(‘Correct palindrome’)
else:
print(‘Not a palindrome’)

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

Remove duplicates from array.

A

def remove_duplicates(array):
unique_array = []
for element in array:
if int(element) not in unique_array:
unique_array.append(int(element))
return unique_array

Test the function
arr = input(“Enter array elements separated by comma: “).split(“,”)
unique_arr = remove_duplicates(arr)
print(“Array with duplicates removed:”, unique_arr)

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

Remove duplicate words.

A

def remove_duplicate_words(sentence):
words = sentence.lower().split()
unique_words = set(words)
result = ‘ ‘.join(unique_words)
return result

sentence = input(“Enter a sentence: “)

modified_sentence = remove_duplicate_words(sentence)
print(“Sentence with duplicate words removed (case-insensitive):”, modified_sentence)

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