Here’s a Python program that meets the specified requirements:
# Ask the user for their address
address = input("Please enter your address: ")
# Store the length of the address
address_length = len(address)
# Print the length of the address
print(f"Your address is {address_length} characters long.")
# Print the fifth character of the address, if it exists
if address_length >= 5:
fifth_character = address[4] # Index 4 for the fifth character
print(f"The fifth character of your address is '{fifth_character}'.")
else:
print("Your address is too short to have a fifth character.")
# Determine if "Main" is in the address
contains_main = "Main" in address
# Print whether "Main" is in the address
if contains_main:
print("The word 'Main' is in your address.")
else:
print("The word 'Main' is not in your address.")
# Print the address three times
print(address * 3)
Explanation of the Code:
-
Address Input: The program prompts the user for their address using
input()
, storing it as astring
, which is the appropriate data type for this purpose. -
Address Length: The length of the address is calculated using
len()
, which returns an integer (the number of characters). This is printed in a formatted string to inform the user about the length of their address. -
Fifth Character: The program checks if the address is long enough to have a fifth character (which is at index 4). If it does, it extracts and prints the fifth character. If not, it notifies the user that their address is too short.
-
Check for "Main": The presence of the word "Main" in the address is checked using the
in
keyword, and the result (a Boolean) is stored incontains_main
. The program then prints whether "Main" is found in the address. -
Print Address Three Times: Finally, the address is printed three times using string multiplication, which is a concise way to repeat strings in Python.
Data Types Used:
- Address: String
- Address Length: Integer
- Fifth Character: String (if exists)
- Contains "Main" Check: Boolean
Feel free to run this code in your Python environment (like PythonAnywhere) to test its functionality.