Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Printing Text in Python

hello world printing

  • Please log in to reply
8 replies to this topic

#1 Root23

Root23

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 141 posts

Posted 18 February 2010 - 04:25 PM

This is a simple tutorial going over the most basic way of printing text in Python. For this tutorial I am using Python 2.6.4. You can get Python here.


To get started we will open up IDLE.


For Windows(XP) machines:
Start -> All Programs -> Python 2.6 -> IDLE (Python GUI)

For Linux(Ubuntu) machines:
Applications -> Programming -> IDLE (Using Python 2.6)


Once IDLE is open, we want to open a new window. This is where we will type all the code for our program. For now you can close Python Shell if you would like. When we run the program the Python Shell will launch automatically.


File -> New Window

Note: Be sure to save your Python files with the .py extension!

To get started typically you want to include a few comments. This can include the program name, possible a few details about the program, and the author name.
#This is a comment. It starts with the pound sign.
#Python will ignore this when executing the code.
#It will only be visible to someone looking at the code.

So, for example, your program may start like this:
#Hello World Program
#This program prints the text 'Hello World' on the users screen
#Created by Author on 01/01/2010

The next step is the program itself. For this we use the print statement.
#Hello World Program
#This program prints the text 'Hello World' on the users screen
#Created by Author on 01/01/2010

print "Hello World"
Now to test this out simply press F5 on your keyboard, or choose Run -> Run Module. You'll be prompted to save your file first, don't forget your .py extension!

If your code is correct the Python Shell should open and display the text "Hello World." It should look like this:
IDLE 2.6.4      
>>> ================================ RESTART ================================
>>> 
Hello World
>>> 

At this point you could be finished, but if you tried running the program in the Command Prompt(for Windows users) you'll notice the program opens and closes in the blink of an eye. To prevent this we will request input from the user. We will use the raw_input statement for this. For now we won't go over the raw_input statement, but you will see one of its uses in your program for now.
#Hello World Program
#This program prints the text 'Hello World' on the users screen
#Created by Author on 01/01/2010

print "Hello World"

raw_input("Press the enter key to exit.")

To see what this does we will press F5 to run this program in the Python Shell. It should look like this:
>>> ================================ RESTART ================================
>>> 
Hello World
Press the enter key to exit.
The program will sit at this point until the user presses the enter key. Once that happens the program ends.

One thing you should know is that the text you were printing above is known as a string. When printing a string you must enclose it with quotation marks. You can use either single or double quotation marks, but be consistent. If you start with single, close with single. If you start with double, end with double. As you can see, we used double quotations in our program.

Hope this helps get some of you started!

-root23-

Edited by Root23, 19 February 2010 - 06:34 PM.

  • 0
Posted Image

#2 James.H

James.H

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 18 February 2010 - 04:26 PM

Nice easy to follow tutorial. +rep!
  • 0

#3 Root23

Root23

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 141 posts

Posted 18 February 2010 - 08:10 PM

Nice easy to follow tutorial. +rep!


Thanks

I didn't realize Saeras had already covered this exact program one of his tutorials. I had only read his Prelude and noticed someone requesting this. Oh well!

Edited by Root23, 19 February 2010 - 08:25 PM.

  • 1
Posted Image

#4 TkTech

TkTech

    The Crazy One

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1144 posts
  • Location:Ottawa, Ontario

Posted 19 February 2010 - 08:41 PM

Multiple insights (in this case, tutorials) into the same thing each provide their own timbits. +rep.

One note, you may want to mention how to exclude newlines from a print statement using a trailing ','
  • 0

#5 Root23

Root23

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 141 posts

Posted 19 February 2010 - 08:48 PM

Thanks

I go over newline suppression in Part 2 that is awaiting approval. Along with repeating, and concatenation.

Multiple insights (in this case, tutorials) into the same thing each provide their own timbits. +rep.

One note, you may want to mention how to exclude newlines from a print statement using a trailing ','


  • 0
Posted Image

#6 TkTech

TkTech

    The Crazy One

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1144 posts
  • Location:Ottawa, Ontario

Posted 19 February 2010 - 08:56 PM

Happily approved. :)
  • 0

#7 Root23

Root23

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 141 posts

Posted 19 February 2010 - 09:21 PM

Thanks again.
  • 0
Posted Image

#8 Guest_Alexander_*

Guest_Alexander_*
  • Guest

Posted 25 March 2010 - 01:11 AM

You could replace raw_input with input if you're working with python 3.
Nice tut
  • 0

#9 k4m1k4z1

k4m1k4z1

    CC Regular

  • Just Joined
  • PipPipPip
  • 35 posts

Posted 19 May 2010 - 06:02 AM

Good tutorial, nice and easy.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download