Create 3 variables to store street, city and country, now create address variable to store entire address. Use two ways of creating this variable, one using + operator and the other using f-string. Now Print the address in such a way that the street, city and country prints in a separate line
Question
Create 3 variables to store street, city and country, now create address variable to store entire address. Use two ways of creating this variable, one using + operator and the other using f-string. Now Print the address in such a way that the street, city and country prints in a separate line
Solution
Sure, here is how you can do it in Python:
# Creating variables
street = "123 Main St"
city = "New York"
country = "USA"
# Creating address variable using + operator
address1 = street + "\n" + city + "\n" + country
print(address1)
# Creating address variable using f-string
address2 = f"{street}\n{city}\n{country}"
print(address2)
In this code, we first create three variables: street, city, and country. Then we create the address1 variable using the + operator to concatenate the street, city, and country variables, with a newline character (\n) in between each to ensure they print on separate lines. We then print address1.
Next, we create the address2 variable using an f-string, which is a way of embedding expressions inside string literals for formatting. We again use the newline character to separate the street, city, and country. Finally, we print address2.
Similar Questions
Write a class that represents a person’s mailing address. It should have separate fields forthe name, street address, city, state and ZIP code. Write an application AddressBook, toincorporate address book application :-(a) To add addresses.(b) Print addresses.
To display number of Recipients from each City. Also write the output
write three address instruction for given (A+B)*(D+E)
print("Day 1 - String Manipulation")print("String Concatenation is done with the "+" sign.")print('e.g. print("Hello " + "world")')print("New lines can be created with a backslash and n.")
Develop a C program that reads the city name and state name from theuser as two different strings. Merge them into a new string (format iscityname@@statename) and display it.Example:Input: Enter city name and state name: Hyderabad TelanganaOutput: Hyderabad@@Telangana
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.