Home > Net >  Print the information in one sentence
Print the information in one sentence

Time:06-30

print all information about an individual in one sentence as follows "[Name] is a [Occupation] for [Number of years] years in [City],[State]."

example input:-

Sunil Shankar Salman

doctor dentist carpenter

15 10 10

Lucknow Delhi Mumbai

Uttar_Pradesh New_Delhi Maharashtra

output:-

Sunil is a Doctor for 15 years in Lucknow, Uttar_Pradesh.

Shankar is a Dentist for 10 years in Delhi, New_Delhi.

Salman is a Carpenter for 10 years in Mumbai, Maharashtra.

**my code** 


name=str(input()) " "
profession = str(input()) "  "
no_of_years=(input()) "  "
cities=str(input()) " "
states=str(input()) " " 

print(f"{name},is a {profession} for  {no_of_years} years  in {cities}, {states}")

CodePudding user response:

I would recommend to use the build-in format-approach instead of the f-string: it is more versatile since you can make a template string which is shared among the data of other "people". The data per person can be collected with a dictionary comprehension which will be passed to the template. Notice the key-value expansion of the dictionary, **.

# form per person

string_template = "{name}, is a {profession} for  {no_of_years} years  in {cities}, {states}"
keys = 'name', 'profession', 'no_of_years', 'cities', 'states'

data = {k: input(f'Insert {k}: ') for k in keys}

print(string_template.format(**data)) # key-value expansion

To further reduce the dependency between the keys and string_format one can use (again) the string-format functionality:

# per person
keys = 'name', 'profession', 'no_of_years', 'cities', 'states'

string_template = "{}, is a {} for  {} years  in {}, {}".format(*map('{{{}}}'.format, keys))
...

Notice the in {{{}}} two brackets are needed for escape the symbol and the last for the substitution with format.

CodePudding user response:

Assuming there is not space between lines in input, Store the details vertically , matrix transpose give matrix with details of person in each row. zip() function helps in transposing the matrix

details_vertical = []
for ctr in range(5):
    details_vertical.append(input().split())
for name, profession, experience, city, state in zip(*details_vertical):
    print(f"{name},is a {profession} for  {experience} years  in {city}, {state}")
  • Related