Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Printing Text in Python - Part 2

hello world printing

  • Please log in to reply
2 replies to this topic

#1 Root23

Root23

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 141 posts

Posted 19 February 2010 - 07:35 PM

This tutorial will build on what you learned in Printing Text in Python - Part 1. For this tutorial I am using Python 2.6.4. You can get Python here.
For instructions on opening up the Python IDLE, please see Part 1 of this tutorial.

In the last tutorial we learned the most basic way of printing text on a users screen with the "Hello World" program. In this tutorial we will cover string concatenation, line suppression, forcing a new line, and repeating strings.

With your new window already open go ahead and add a few comments so you, or someone else looking at your code, will know what the program is. Here is an example of what I used:
# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

Concatenation:


To concatenate strings we will use the '+' operator. So let's say the first line of your program is as follows:

# CodeCall Program
# This program shows how to concatenate strings, and repeat strings.
# Author 01/01/2010

print "Welcome"

That's simple enough. But let's use the '+' operator to add to it.
# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

print "Welcome" + "CodeCall!"

Everything looks correct, so let's run the program and see what we get.
IDLE 2.6.4      
>>> ================================ RESTART ================================
>>> 
WelcomeCodeCall!
>>> 

Hopefully you noticed a problem with the printed output. There is no space between "Welcome" and "CodeCall!". This is because when using the '+' operator you must put the space in the string. So if we revise our code to:
# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

print "Welcome " + "CodeCall!"
It should now print correctly. If you want you could have put the space in front of "CodeCall!" and had the same result. It's that simple. Also, you can concatenate as many strings as you wish. For example:
# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

print "Welcome " + "CodeCall!" + " This" + " is a " + "Python tutorial."
Would result in:
IDLE 2.6.4      
>>> ================================ RESTART ================================
>>> 
Welcome CodeCall! This is a Python tutorial.
>>> 
Concatenation may seem pointless at this point, but once you start working with variables it will make more sense.


Suppression:


When working with multiple print statements, Python automatically starts each print statement on a new line. Sometimes you may want the printed statements to be on the same line, and to accomplish this we use a comma at the end of the previous print statement. It looks something like this:

# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

print "Welcome " + "CodeCall!",
print "This is a Python tutorial."
Notice the comma is placed at the end of the string, and not inside it. The resulting output is the same as with string concatenation:
IDLE 2.6.4      
>>> ================================ RESTART ================================
>>> 
Welcome CodeCall! This is a Python tutorial.
>>> 
You can also use the comma in a similar manner as the '+' operator:
# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

print "Welcome " + "CodeCall!", "This is a Python Tutorial."
Notice the output is still the same as before, but also notice we didn't have to include a space in the string when using the comma like we did when using the '+' operator.


Forcing a New Line:


Since I've shown you how to suppress a new line, it is only fair that I show you how to force a new line. I'm going to show you two different examples. Both example use the same escape sequence '\n'.

For the first example we'll split a single string onto multiple lines:

# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

print "Welcome " + "CodeCall! \nThis is a Python Tutorial."
Notice the placement of the '\n' is butted up to one of the words in the string. It doesn't matter that the escape sequence is touching one of the words. Here is the result of our code:
IDLE 2.6.4      
>>> ================================ RESTART ================================
>>> 
Welcome CodeCall! 
This is a Python Tutorial.
>>> 

Now what if you want to have a blank line between two separate printed strings? That's just a simple. Since two separate print statements already start on separate lines, the escape sequence simply adds a blank line. Take a look at the next example code:
# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

print "Welcome " + "CodeCall!\n"
print "This is a Python Tutorial."
This will give us the following result:
IDLE 2.6.4      
>>> ================================ RESTART ================================
>>> 
Welcome CodeCall!

This is a Python Tutorial.
>>> 
One thing to keep in mind is that the escape sequence must be part of the string. If you try placing it outside of the string you will get an EOF error. Give it a shot to see what happens.



Repeating:


What if you want to repeat a string multiple times. Well that's just as simple as everything you've learned so far. We'll use the '*' operator to accomplish this:

# CodeCall Program
# This program shows different ways to concatenate strings, and repeat strings.
# Author 01/01/2010

print "CodeCall! " * 5
This results in:
IDLE 2.6.4      
>>> ================================ RESTART ================================
>>> 
CodeCall! CodeCall! CodeCall! CodeCall! CodeCall! 
>>> 
Notice I did include a space in the string. Otherwise "CodeCall!" would be printed together, with no space.


Hopefully this helps a few of you out. Goodluck!

Edited by Root23, 19 February 2010 - 09:16 PM.
added new section(Forcing a New Line)

  • -1
Posted Image

#2 UnknownFear

UnknownFear

    CC Regular

  • Just Joined
  • PipPipPip
  • 45 posts

Posted 07 March 2010 - 02:22 PM

This was very helpful! I am learning Python and this will come in handy :) Thanks!
  • 0

#3 Madhak

Madhak

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 04 March 2011 - 07:06 AM

Hey thanks man :) this helped alot, You have good Tutorials :D
  • 0