Strings Flashcards
Use a for loop with an integer counter to print the following ten lines:
1 little Indians
2 little Indians
…
10 little Indians
for counter in range(1, 11):
print(str(counter) + “ little Indians”)
Join all path parts with backslash signs (\) and print the entire string.
path_parts = [‘c:’,’users’,’john’,’images’,’camera’,’edited’,’001.img’]
print(‘\'.join(path_parts))
Use ‘join’ to print the countries “I have visited”:
visited_countries = [‘Norway’, ‘Sweden’, ‘Denmark’, ‘Germany’, ‘France’]
visited_countries = [‘Norway’, ‘Sweden’, ‘Denmark’, ‘Germany’, ‘France’]
separator = ‘, ‘
print(‘I have visited the following countries: ‘ + separator.join(visited_countries))
Write code that prints your name and your age.
user_name = input(“Enter your name: “)
user_age = int(input(“Enter your age: “))
greeting = user_name + ‘, you are ‘ + str(user_age) + ‘!’
print(greeting)
Print email address. If not a valid email address, print “Not a correct email address”.
user_input = input(‘Enter your email address: ‘)
if ‘@’ not in user_input:
print(‘Not a correct email address!’)
else:
print(‘Thank you’)
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!
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!’)
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.
username = input(‘To register, enter a username without digits: ‘)
for character in username:
if character.isdigit():
print(character + ‘ is a digit!’)
Count and print how many times the letter ‘e’ appears in a poem.
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.”)
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!
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!”)
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.
print(pi[2:12])
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.’
]
for log in logs:
print(log[6:])
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!’
print(secret_message[1::2])
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.
if palindrome == palindrome[::-1]:
print(‘Correct palindrome’)
else:
print(‘Not a palindrome’)
Remove duplicates from array.
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)
Remove duplicate words.
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)