[Tutor] How to use a Variable in an input statement
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Jul 1 09:37:00 EDT 2020
On 01/07/2020 13:42, YAW ANTWI-ADJEI wrote:
> Thus, I want something like:
> *StudentName* = 'Yaw'
> Age = input(*StudentName*, 'how old are you?')
There are several ways to do this, but string formatting is
probably best:
age = input("{} how old are you? ".format(Studentname))
OR
age = input("%s how old are you? " % StudentName)
Or even, in recent pythons(post 3.6):
age = input(f"{StudentName} how old are you? ")
The last one is preferred if you don't have to support older versions.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list