More Variable and Printing

Now we’ll do even more typing of variables and printing them out. This time we’ll use “format string.” Every time you put " (double-quotes) around a piece of text, you have been making a string. You print them, save them to files, send them to web servers, all sorts of things. 

Strings are really handy, so in this tutorial, you will learn how to make strings that have variables in them. You embed variables inside a string by using specialized format sequences and then putting the variables at the end with a special syntax that tells Python, “Hey, this is a format string, put these variables in there.” 

Now, let's understand more using the below example.

Exercise 1:

PyGhostNow, let's understand this code. First, we defined Variables which are simple String and Integer. Second, we print them while embedding them in a string. 

  • Line number 10, %s can be used to define a string-type variable. 
  • Line number 11, %d can be used for Integer type variable. 
  • Line 14, We defined multiple variables in the same string. 
  • Line 17, we used maths while passing the variables to a string.

Time to run this code to get output.


Exercise 2:

Now we are going to do some more exercise regarding Variable and Printing. We can multiply sting to print it multiple time.


The output of above code will be:


"." Will be printed 10 times. You will understand this with below example.

The output will be:

Here # is printed 13 (6 + 7) times.

Exercise 3:

Printing New lines with python using a single variable or print command.


You can use "\n" to enter a blank line while printing.

Some more escape What it does.

  1. \\ Backslash (\)

  2. \' Single-quote (')

  3. \" Double-quote (")

  4. \a  ASCII bell (BEL)

  5. \b  ASCII backspace (BS)

  6. \f ASCII formfeed (FF)

  7. \n ASCII linefeed (LF)

  8. \N{name} Character named name in the Unicode database (Unicode only)

  9. \r ASCII carriage return (CR)

  10. \t ASCII horizontal tab (TAB)

  11. \uxxxx Character with 16-bit hex value xxxx (Unicode only)

  12. \Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (Unicode only)

  13. \v ASCII vertical tab (VT)

  14. \ooo Character with octal value oo

  15. \xhh Character with hex value hh


About the author



Please login to comment