Home > Software design >  How can I print the input values in between a line?
How can I print the input values in between a line?

Time:11-06

name = input(enter name)
age = input(age)

print(“My name is print(name). I’m print(age) years old.”)

Nobbie experiment. Beginner level task. And the above query came to my mind.

CodePudding user response:

You could also use f-string formatting once you have capture the input in name and age variable.

print(f"My name is {name}. I'm {age} years old")
My name is laura. I'm 18 years old

CodePudding user response:

You can simply use python f-strings.

name = input('Enter your name ')
age = input ('Enter your age ')

print(f'My name is {name}. I\'m {age} years old.')

If you also want you can go with the format function.

CodePudding user response:

or this

name = input('Enter your name ')
age = input ('Enter your age ')
print('some text'   name   'also some text') 

good luck at programming,

CodePudding user response:

You can use a formatted string for this:

def name_and_age(name, age):
    return f"My name is {name} and I'am {age} years old"
print(name_and_age('Max', 35))
#output is My name is Max and I'am 35 years old

CodePudding user response:

name = input("enter name: ")
age = input("age: ")
print(f"My name is {name}. I am {age} years old")

CodePudding user response:

You can also try string formatting.

name = input('Enter your name ')
age = input ('Enter your age ')

print('My name is {}. I\'m {} years old.').format(name,age)

More About String Formatting

CodePudding user response:

Would be a good idea to take some time to read a Python book.

name = input("enter name")
age = input("age")

print( f"My name is {name}. I'm {age} years old.")

CodePudding user response:

Study and try to understand Keshav V. answer using f-strings, this is the "modern" approach and will serve you well time after after time.

A more long handed approach would be to rewrite your program like this (as a stepping stone to understanding the f-string format)..

name = input("enter name ")
age = input("age ")

print("My name is", name, "I’m", age, "years old.")

Notice that print will accept as many items as you want to print and place them on the same line.

  • Related