Python · Syntax · Beginner
Conditions 2
Additional practice with conditionals in Python: string checks, text lengths, characters, slices, string methods, logical and/or operators, and simple input processing scenarios.
Quick topic start and explanations before exercises (exercises below):
Patterns from the exercises
#String conditions quick reference
#Exercises:
First and last character.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if len(text) > 10:
print(text[0], text[-1])
else:
print("The string is too short")
# or you can do this: store the characters in variables
text = input("Enter a string: ")
if len(text) > 10:
first = text[0]
last = text[-1]
print(first, last)
else:
print("The string is too short")
String starts with A.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if text.startswith("A") or text.startswith("a"):
print("Starts with A")
else:
print("Starts with another letter")
# OR
if text[0] == "A" or text[0] == "a":
print("Starts with A")
else:
print("Starts with another letter")
# OR
if text[0].lower() == "a":
print("Starts with A")
else:
print("Starts with another letter")
As a result of this expression text[0], we get a new string object that
consists of 1 character, which means we can apply methods to it.
First four characters.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if len(text) > 8:
print(text[:4])
else:
print(text)
# or you can do this: prepare the fragment first
text = input("Enter a string: ")
if len(text) > 8:
part = text[:4]
else:
part = text
print(part)
Emotional message.
#text = input("Enter a message: ")
Solution
text = input("Enter a message: ")
if len(text) > 6 and text[-1] == "!":
print("Emotional message")
else:
print("Regular message")
# or you can do this using the endswith method
text = input("Enter a message: ")
if len(text) > 6 and text.endswith("!"):
print("Emotional message")
else:
print("Regular message")
Searching for special characters.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if "@" in text or "#" in text:
print("Special character found")
else:
print("No special characters")
# or you can do this: check the characters one by one
text = input("Enter a string: ")
if "@" in text:
print("Special character found")
elif "#" in text:
print("Special character found")
else:
print("No special characters")
Greeting by first and last name.
#name = input("Enter first name: ")
surname = input("Enter last name: ")
Solution
name = input("Enter first name: ")
surname = input("Enter last name: ")
if name and surname:
print(f"Hello, {name} {surname}!")
else:
print("Enter valid data")
# or without an f-string, using string concatenation
name = input("Enter first name: ")
surname = input("Enter last name: ")
if name and surname:
print("Hello, " + name + " " + surname + "!")
else:
print("Enter valid data")
Slice from the 3rd to the 8th character.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if len(text) > 10:
print(text[2:8])
else:
print("Not enough characters")
First and last in a message.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if text:
print(f"First character: {text[0]}, last character: {text[-1]}")
else:
print("Empty string")
# or you can do this: save the first and last character beforehand
text = input("Enter a string: ")
if text:
first = text[0]
last = text[-1]
print(f"First character: {first}, last character: {last}")
else:
print("Empty string")
Suitable string length.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if 5 <= len(text) <= 10:
print("Suitable length")
else:
print("Unsuitable length")
# second option: the same thing without chained comparisons
text = input("Enter a string: ")
length = len(text)
if length >= 5 and length <= 10:
print("Suitable length")
else:
print("Unsuitable length")
Looks like Python.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if text.startswith("Py") or text.endswith("on"):
print("Looks like Python")
else:
print("Does not look like Python")
# or you can do this if the case of the first letter does not matter
text = input("Enter a string: ")
if text.lower().startswith("py") or text.endswith("on"):
print("Looks like Python")
else:
print("Does not look like Python")
Hello or goodbye.
#text = input("Enter a phrase: ")
Solution
text = input("Enter a phrase: ")
if text.lower() == "hello":
print("Hello to you too!")
elif text.lower() == "bye":
print("Adios!")
else:
print("Nah, I don't talk to strangers! Adios!")
# OR more carefully, to account for containment instead of exact equality:
if "hello" in text.lower():
print("Hello to you too!")
elif "bye" in text.lower():
print("Adios!")
else:
print("Nah, I don't talk to strangers! Adios!")