Concatenating and Repeating Strings


You've seen how you can insert special characters into a string, but there are things you can do with entire strings themselves. You can combine two separate strings into a larger one. And you can even repeat a single string as many times as you want.

Introducing the Silly Strings Program

The Silly Strings program prints several strings to the screen. The results are shown in Figure 2.4.

click to expand
Figure 2.4: The strings on the screen appear differently than in the program code.

Though you've already seen strings printed, the way these strings were created is brand-new to you. Take a look at the code:

 # Silly Strings # Demonstrates string concatenation and repetition # Michael Dawson - 1/11/03 print "You can concatenate two " + "strings with the '+' operator." print "\nThis string " + "may not " + "seem terr" + "ibly impressive. " \       + "But what " + "you don't know," + " is that " + "it's one real" \       + "l" + "y" + " long string, created from the concatenation " \       + "of " + "thirty-two " + "different strings, broken across " \       + "nine lines." + " Now are you" + " impressed?\n\n" + "See, " \       + "even newlines can be embedded into a single string, making" \       + " it look " + "as " + "if " + "it" + "'s " + "got " + "to " \       + "be" + " multiple strings." + " Okay, now this " + "one " \       + "long" + " string " + "is over!" print \ """ If you really like a string, you can repeat it. For example, who doesn't like pie? That's right, nobody. But if you really like it, you should say it like you mean it:""", print "Pie" * 10 print "\nNow that's good eating." raw_input("\n\nPress the enter key to exit.") 

Concatenating Strings

Concatenating strings means joining them together, to create a whole new string. A simple example is in the first print statement:

 print "You can concatenate two " + "strings with the '+' operator." 

The + operator joins the two strings, "You can concatenate two " and "strings with the '+' operator.", together to form a new, larger string. It's pretty intuitive. It's like adding the strings together using the same symbol you've always used for adding numbers.

TRAP

When you join two strings, their exact values are fused together, with no space or separator character inserted between them. So, if you were to join the two strings "cup" and "cake", you'd end up with "cupcake" and not "cup cake". In most cases, you'll want to insert a space between strings you join, so don't forget to put one in.

The next print statement shows that you can concatenate 'till your heart's content:

 print "\nThis string " + "may not " + "seem terr" + "ibly impressive. " \       + "But what " + "you don't know," + " is that " + "it's one real" \       + "l" + "y" + " long string, created from the concatenation " \       + "of " + "thirty-two " + "different strings, broken across " \       + "nine lines." + " Now are you" + " impressed?\n\n" + "See, " \       + "even newlines can be embedded into a single string, making" \       + " it look " + "as " + "if " + "it" + "'s " + "got " + "to " \       + "be" + " multiple strings." + " Okay, now this " + "one " \       + "long" + " string " + "is over!" 

The computer prints one long string that was created by the concatenation of 32 individual strings. One thing you may notice is that the string doesn't correctly wrap in the console window. So be careful when you create super-long strings.

Suppressing a Newline

You've seen how you can add extra newlines with the \n escape sequence. But you can also suppress a newline so that the text of two consecutive print statements appears on the same line. All you have to do is add a comma to the end of a print statement, like so:

 print \ """ If you really like a string, you can repeat it. For example, who doesn't like pie? That's right, nobody. But if you really like it, you should say it like you mean it:""", 

By adding the comma at the end of this triple-quoted string, the next text printed will appear on the same line as say it like you mean it:.

Repeating Strings

The next new idea presented in the program is illustrated in the following line:

 print "Pie" * 10 

This line creates a new string, "Pie Pie Pie Pie Pie Pie Pie Pie Pie Pie", and prints it out. That's the string "Pie" repeated 10 times, by the way.

Like the concatenation operator, the repetition operator, *, is pretty intuitive. It's the same symbol used for multiplying numbers on a computer, so repeating a string with it makes sense. It's like you're multiplying the string. You can repeat a string as many times as you want. To repeat a string, just put the string and number of repetitions together with the repetition operator, *.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net