Python Lab 6a: Python's string operations
1. String operations create a new string Write a code that asks the user to enter a phrase. If the phrase
based on the first. equals the same phrase in uppercase, tell the user 'Stop
shouting please!'
word1 = 'Pizza' Enter a phrase: Nice to see you
print([Link]())
# prints PIZZA Enter a phrase: NICE TO SEE YOU
print([Link]()) Stop shouting please!
# prints pizza
Two strings can be checked for equality
using the == operator.
print('house' == 'hat')
# prints out False
2. The in command can check to see if Write a code that asks the user to enter a string.
a certain character is in a string. Count the number of different vowels ( a, e, i, o, u) that are in the
string and print out the total.
if ('e' in 'house'): You may need to write 5 different if statements, one for each
count = count + 1 vowel.
Enter a string: mouse
mouse has 3 different vowels
3. Relational operators, such as < and > Write code that asks the user to enter two strings. Store the
can be used with strings. One string is strings in two different variables.
"less than" another if it comes before the Print out the string that comes first in alphabetical order.
string when they are placed in
alphabetical order. Enter a string: mouse
Enter another string: cat
print('mouse' < 'cat') cat comes before mouse
# prints out False
4. Two strings can be checked for A certain website asks people making an account to enter their
inequality using the != operator. email address twice. Write code that asks the user to enter their
email address. Ask them to enter it again. Then, while the two
print('house' != 'hat') strings are not equal, print out a message stating that the two
# prints out True inputs are not equal, and ask the user to enter each again. Print
print('house' != 'House') out 'Thank You!' at the end of the program.
# prints out True
Enter your email address: person@[Link]
Enter your email address again: persn@[Link]
The two inputs did not match.
Enter your email address: person@[Link]
Enter your email address again:
person@[Link]
Copyright © 2019 Pearson Education, Inc. or affiliate(s). All rights reserved.
Thank you!
Reference codes:
6a-1:
phrase = input("Enter a phrase: ")
# Check if the original phrase equals its uppercase version
if phrase == [Link]():
print("Stop shouting please!")
6a-2:
user_string = input("Enter a string: ").lower()
# Initialize counter for different vowels
vowel_count = 0
if 'a' in user_string:
vowel_count += 1
if 'e' in user_string:
vowel_count += 1
if 'i' in user_string:
vowel_count += 1
if 'o' in user_string:
vowel_count += 1
if 'u' in user_string:
vowel_count += 1
print(f"{user_string} has {vowel_count} different vowels")
6a-3:
string1 = input("Enter a string: ")
string2 = input("Enter another string: ")
# Compare strings using relational operators
if string1 < string2:
print(f"{string1} comes before {string2}")
elif string1 > string2:
print(f"{string2} comes before {string1}")
else:
print("The two strings are identical")
Copyright © 2019 Pearson Education, Inc. or affiliate(s). All rights reserved.
6a-4:
email1 = input("Enter your email address: ")
email2 = input("Enter your email address again: ")
# Use while loop to keep asking until emails match
while email1 != email2:
print("The two inputs did not match.")
print()
# Ask user to enter both emails again
email1 = input("Enter your email address: ")
email2 = input("Enter your email address again: ")
# Thank user when emails match
print("Thank you!")
Copyright © 2019 Pearson Education, Inc. or affiliate(s). All rights reserved.