Text input and output

Output
To output text to the screen you will need one line of code:

print("Hello World")

To write multiple lines, add the ‘\n’ character:

print("Hello World\nThis is a message")

To print variables:

x = 3
print(x)

To print multiple variables on one line:

x = 2
y = 3
print("x = {}, y = {}".format(x,y))

Input
To get a text value:

name = input("Enter a name: ")

To get a whole number:

x = int(input("What is x? "))

To get a decimal number:

x = float(input("Write a number"))

Getting started
If statements

15 thoughts on “Text input and output

  1. Reply
    Joseph - January 30, 2016

    simple, straightforward and very clear. Am so new to python this is the best tutorial have come across. Thank you guys

  2. Reply
    Ricardo - February 4, 2016

    This was the easiest way to start learning python. I can’t wait to learn more.

  3. Reply
    nimda - April 29, 2016

    Print on 1 line
    print (“a”,end=” “)
    print (“b”,end=” “)

  4. Reply
    Ken - May 6, 2016

    Very good tutorials,straight to to point and understanding what each line does.

  5. Reply
    Dinuk - May 8, 2016

    How am I erase other python codes on cmd.(what’s the code to erase)

    1. Reply
      admin - May 18, 2016

      Run your python script as a file, such as ‘python hello.py’.
      Then on windows:

      import os
      os.system('cls') # on windows

      on linux:

      import os
      os.system('clear')

  6. Reply
    ldc - May 20, 2016

    There is a problem when you write
    name = input(“What your name”)
    and you must type the string with ” or “”, or else you can get NameError

    1. Reply
      Chintan Patel - June 20, 2016

      instead you can use
      name=raw_input(“whats your name”)

  7. Reply
    Lee Wang - May 30, 2016

    I used “name = input(“Enter your name:”)
    print(name)
    when i input name is number, print is ok, but when i input a char, print error message” name = input(‘Enter your name:’)
    File “”, line 1, in
    NameError: name ‘a’ is not defined”

    why?

    1. Reply
      admin - July 22, 2016

      Use raw_input for your Python version

  8. Reply
    Ransom - July 18, 2016

    I have the NINJA-IDE installed on my windows computer right now and I tried entering the first line shown here with no result when I press run project. Am I doing something wrong or maybe I am just not using it right? I am brand new to this whole thing and could really use a helping hand if anyone would be willing. Thanks in advance and sorry for the long post.

    1. Reply
      admin - July 22, 2016

      In the menu bar on top, press “project,run file” or “project,run project”

  9. Reply
    ForeverBird - August 17, 2016

    the code of print multiple variables show be below:
    x = 2
    y = 3
    print(x + ‘ ‘ + y)

    if the last line of the code is
    print(x, ‘ ‘, y)
    the text of output is
    (2, ‘ ‘, 3)
    Please correct it, thank you

    1. Reply
      admin - August 20, 2016

      Resolved

  10. Reply
    David - November 5, 2016

    Thanks for simple and descriptive script.

Leave a Reply

Your email address will not be published. Required fields are marked *