[Tutor] Formatting Prompts

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jul 13 18:56:34 EDT 2020


On 13/07/2020 20:36, 1611kjb at gmail.com wrote:
> The input() function allows for a prompt to be inserted to provide
> information to the user. Can this string be formatted or does it just accept
> ascii text?

Strings in Python are unicode. They are also objects so have methods.
One such method is the format() method so you can do things like:

> Counter += 1
> input("This is attempt {} ".format(counter))

Or if you have a recent version of python use a format string:

input(f"This is attempt {counter}")

But you can generate the string in any way, including
stored variables or the return value from a function:

input(do_fancy_formatting() )

So far as input is concerned its just a regular string.
How you build that string is up to you.

-- 
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