![]() Back to www.deitel.com |
1 # Fig. 35.10: fig35_10.py
|
2 # Program to illustrate use of strings
|
3
|
4 # simple string assignments
|
5 string1 = "This is a string."
|
6 print string1
|
7
|
8 string2 = "This is a second string."
|
9 print string2
|
10
|
11 # string concatenation
|
12 string3 = string1 + " " + string2
|
13 print string3
|
14
|
15 # using operators
|
16 string4 = '*'
|
17 print "String with an asterisk: " + string4
|
18 string4 *= 10
|
19 print "String with 10 asterisks: " + string4
|
20
|
21 # using quotes
|
22 print "This is a string with \"double quotes.\""
|
23 print 'This is another string with "double quotes."'
|
24 print 'This is a string with \'single quotes.\''
|
25 print "This is another string with 'single quotes.'"
|
26 print """This string has "double quotes" and 'single quotes.'"""
|
27
|
28 # string formatting
|
29 name = raw_input( "Enter your name: " )
|
30 age = raw_input( "Enter your age: " )
|
31 print "Hello, %s, you are %s years old." % ( name, age )
|
This is a string.
This is a second string.
This is a string. This is a second string.
String with an asterisk: *
String with 10 asterisks: **********
This is a string with "double quotes."
This is another string with "double quotes."
This is a string with 'single quotes.'
This is another string with 'single quotes.'
This string has "double quotes" and 'single quotes.'
Enter your name: Brian
Enter your age: 33
Hello, Brian, you are 33 years old.
|