How do you print out the input you put in? - python

I am writing a piece of code which asks for the users first name then second name and then their age but then i print it out but I'm not sure how to please give an answer.
print ("What is your first name?"),
firstName = input()
print ("What is you last name?"),
secondName = input()
print ("How old are you?")
age = input()
print ("So, you're %r %r and you're %r years old."), firstName, secondName, age

You want to use string.format.
You use it like so:
print ("So, you're {} {} and you're {} years old.".format(firstName, secondName, age))
Or in Python 3.6 upwards, you can use the following shorthand:
print (f"So, you're {firstName} {secondName} and you're {age} years old.")

Use
print ("So, you're %r %r and you're %r years old." % (
firstName, secondName, age))
You might want to consider using %s for the strings and %d for the number, though ;-)

New style string formatting in Python:
print("So, you're {} {} and you're {} years old.".format(firstName, secondName, age))

This is the fixed code:
firstName = input("What is your first name?")
secondName = input("What is you last name?")
age = input("How old are you?")
print ("So, you're " + firstName + " " + secondName + " and you're " + age + " years old.")
It is pretty easy to understand, since this just uses concatenation.

There are two ways to format the output string:
Old way
print("So, you're %s %s and you're %d years old." % (firstName, secondName, age)
New way (Preferred way)
print("So, you're {} {} and you're {} years old.".format(firstName, secondName, age))
The new way is much more flexible and provides neat little conveniences like giving placeholders an index. You can find all the differences and advantages here : PyFormat

Related

How can I print the input values in between a line?

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.
name = input("enter name: ")
age = input("age: ")
print(f"My name is {name}. I am {age} years old")
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.
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
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.
or this
name = input('Enter your name ')
age = input ('Enter your age ')
print('some text' + name + 'also some text')
good luck at programming,
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
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.")
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: My name is Max and I'am 35 years old

How to add a space between 2 inputs in print?

So the code I'm doing is this:
first_name = input("What is your first name? ")
last_name = input("What is your last name? ")
print("Hi there " + first_name + last_name)
What I want to do is make a space so when I run it it shows a space between first_name and last_name.
When I run the program, it shows:
What is your first name? px1se
What is your last name? unknown
Hi there px1seunknown
I wanna get a space between the "px1se" and the "unknown".
For your print statement, try the f-string:
print(f'Hi there {first_name} {last_name}')
Here is the documentation
Or, alternatively,
print("Hi there " + first_name + " " + last_name)
or
print("Hi there {} {}".format(first_name, last_name))
try :
print("Hi there " + first_name +" "+ last_name)
Plenty of sensible approaches offered already, but I thought it might be worth adding the old style formatting to the list.
print("Hi there %s %s"%(first_name,last_name))

How to get user input? Python

I'm very new to this, so please be gentle!
As a little bit of work for my Python course, I am learning to run user input code. I put together the below code but when I run it using command-B, it asks me the 'What's your name?' question, but when I type in my name and click enter, nothing happens? For info, I am using Python 3.7, and using SublimeText.
I am 100% sure this is an easy answer, but surprisingly I cannot find the answer, and I have searched a little bit on here, and generally via Google, etc.
name=input("What's your name?:")
print("Hi",name,"how do you do.")
age=input("How old are you",name,"?:")
print("Great",name,"I'm",age,"years old too.")
city=input("Which city do you come from",name,"?:")
print("What a coincidence, I am from",city,"too.")
print(name,", here is your record //")
print(name, age, city)
Thanks for any help, and if you guys have an tips for a super newbie, it would be much appreciated!
You are having some issues because you have few syntax errors. The following is your code with the corrections. There you go:
name = input("What's your name?:")
print("Hi "+ name + " how do you do.")
age = input("How old are you " + name + " ?:")
print("Great " + name + "I'm " + age + " years old too.")
city = input("Which city do you come from " + name + " ?:")
print("What a coincidence, I am from " + city + " too.")
print(name + " , here is your record //: ")
print(name + " " + age + " " + city)
Good luck with learning Python :)
a = input() //for string
b = int(input()) //for int
c = float(input()) //for float
name=input("What's your name?:")
print("Hi"+name+"how do you do.")
age=input("How old are you "+name+" ?:")
print("Great "+name," I'm ",age+" years old too.")
city=input("Which city do you come from "+name+" ?:")
print("What a coincidence, I am from "+city+" too.")
print(name+", here is your record //")
print(name+age+city)
Use + for concatenation inside print

Regarding the string format in python, what does '[%s]' mean?

I saw a line of code:
re.compile('[%s]' % re.escape(string.punctuation))
But I have no idea about the function of [%s]
Could anyone help me please?
Thank You!
It is a string formatting syntax (which it borrows from C).
Example:
name = input("What is your name? ")
print("Hello %s, nice to meet you!" % name)
And this is what the program will look like:
What is your name? Johnny
Hello Johnny, nice to meet you!
You can also do this with string concentation...
name = input("What is your name? ")
print("Hello + name + ", nice to meet you!")
...However, the formatting is usually easier.
You can also use this with multiple strings.
name = input("What is your name? ")
age = input("How old are you? ")
gender = ("Are you a male or a female? ")
print("Is this correct?")
print("Your name is %s, you are %s years old, and you are %s." % name, age, gender)
Output:
What is your name? Johnny
How old are you? 37
Are you a male or a female? male
Is this correct?
Your name is Johnny, you are 37 years old, and you are male.
Now, please, before asking any more questions, see if they exist!

How would I take a user's input and reprint it in the Python shell

I'm fairly new to Python, and I'm working on a few things that would involve names. Something such as:
example = raw_input ("What is your name?: ")
How would I go about using this to have them answer it, and reprint their name such as:
print "Hello, {name}!"
You were very close, you already had the correct template, now you have to tell it what the {name} should be replaced by:
>>> example = "Foo"
>>> "Hello, {name}!".format(name=example)
'Hello, Foo!'
See the docs for more information on using str.format.
print "Hello, " + example + "!"
There are several ways to acomplish your goal.
As was mentioned, you could use:
name = raw_input ("What is your name?: ")
print "Hello, " + name + "!"
or,
>>> example = "Foo"
>>> "Hello, {name}!".format(name=example)
'Hello, Foo!'
You could also use,
name = raw_input("What is your name?: ")
print "Hello, %s!" % name
print raw_input('Enter name :')
For Python 3.5 users, it can work like this:
name = input("What is your name? ")
print "Hello, %s!" % (name)
That should work.
name = raw_input ("What is your name?: ")
print "Hello, " + name + "!"

Categories