By: Zed A. Shaw in python Tutorials on 2011-03-04
A string is usually a bit of text you want to display to someone, or "export" out of the program you are writing. Python knows you want something to be a string when you put either " (double-quotes) or " (single-quotes) around the text.
You saw this many times with your use of print when you put the text you want to go to the string inside " or " after the print. Then Python prints it. Strings may contain the format characters you have discovered so far. You simply put the formatted variables in the string, and then a % (percent) character, followed by the variable. The only catch is that if you want multiple formats in your string to print multiple variables, you need to put them inside () (parenthesis) separated by , (commas). It's as if you were telling me to buy you a list of items from the store and you said, "I want milk, eggs, bread, and soup." Only as a programmer we say, "(milk, eggs, bread, soup)".
We will now type in a whole bunch of strings, variables, formats, and print them. You will also practice using short abbreviated variable names. Programmers love saving themselves time at your expense by using annoying cryptic variable names, so let's get you started being able to read and write them early on.
1 x = "There are %d types of people." % 10 2 binary = "binary" 3 do_not = "don't" 4 y = "Those who know %s and those who %s." % (binary, do_not) 5 6 print x 7 print y 8 9 print "I said: %r." % x 10 print "I also said: '%s'." % y 11 12 hilarious = False 13 joke_evaluation = "Isn't that joke so funny?! %r" 14 15 print joke_evaluation % hilarious 16 17 w = "This is the left side of..." 18 e = "a string with a right side." 19 20 print w + e
What You Should See
$ python ex6.py There are 10 types of people. Those who know binary and those who don't. I said: 'There are 10 types of people.'. I also said: 'Those who know binary and those who don't.'. Isn't that joke so funny?! False This is the left side of...a string with a right side. $
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in python )
Latest Articles (in python)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews