String Constants

 Strings are a collection of characters which are stored together to represent arbitrary text inside a python program. You can create a string  constant inside a python program by surrounding text with either single quotes (’),
double quotes ("), or a collection of three of either types of quotes (’’’ or"""). In the first two cases, the opening and closing quotes must appear on the same line in your program; when you use triple quotes, your text can span as many lines as you like. The choice of which quote symbol to use is up to you – both single and double quotes have the same meaning in python.
Here are a few examples of how to create a string constant and assign its value to a variable:

name = ’Phil’
value = "$7.00"
helptext = """You can create long strings of text
spanning several lines by using triple quotes at
the beginning and end of the text"""

When the variable helptext is printed (using the print command, it would display as three lines, with the line breaks at the same points as in the triple-quoted text.
You can also create strings by reading input from a file or by concatenating smaller strings.
Special Characters in Strings


Post a Comment

0 Comments