Simple Python Tutorial for printing formatted output

Veröffentlicht am: 05 April 2023
auf dem Kanal: Earth Walker
339
11

Simple python example and short video to illustrate the use of print() display formatted message on the console. It shows 4 different methods of using the print() to display output. The difference between using comma (,) and using string concatenation. It also shows how to use modulo (%) to display string in required number of decimal places. There is also an example to use fstring method to display.The examples used are very simple so that it is easy for beginner to understand.

The 4 methods discussed has the following syntax
print( " ... , varInt , ... , varFloat , ... " )
print( " ... + str(varInt) + ... + str(varFloat) + ... " )
print( " ... %d ... %.2f ... " % (var1,var2) )
print( f" ... {varInt} ... {varFloat:.2f} ... " )

The below are the codes used
############
name = "Mary"
age = 18
height = 1.777

print(name + " is " + str(age) + " years old and "+ str(height) +" m tall" )
print(name , "is" , age , "years old and", height ,"m tall" ) #add space between
print("%s is %d years old and %.2f m tall" % (name,age,height) )
print(f"{name} is {age} years old and {height:.2f} m tall")
############

Beginner may get confused with the comma and string concatenation method.
To print with a mixture of numerical (int and float) variables and string variables, we need to use str() for the string concatenation (+) method.

Note that the string concatenation (+), Modulo (%) and fstring method are just few ways to format a string.
You can also use them in input() as well to display a string on the screen to prompt user for input.

You can use the link below to see video on how to use input()
Apart from watching the video, you can also show more info to read on the explanation.
   • Simple Tutorial Python getting user input ...  

Please like the video if it helps you :)

Hope these inputs are helpful for you.

0:00 - print() using string concatenation (+) and comma (,)
05:14 - print() using modulo (%) operator method
06:45 - print() using fstring method


Keywords:
#python
#string
#fstring
#print()
#str()
#concatenation
#comma
#modulo
#tutorial
#short
#easy
#+
#percent
#%


Auf dieser Seite können Sie das Online-Video Simple Python Tutorial for printing formatted output mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Earth Walker 05 April 2023 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 339 Mal angesehen und es wurde von 11 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!