So, let's start of with some basic strings and print statements. If you want Python to print something, you just type this into a python interactive shell:
print 'message goes here'
When you use print, you are passing it a string between the ' '. This string is then printed underneath the command. But what if you want to use ' in your message? well, python will also accept " as quotation marks.
print "message goes here"So, we know how to print just one line of stuff, but what if you want to print a few lines of stuff? Well, for this you can use three " to tell python that you want to use multiple lines.
print """Lots and lots of lines more lines Another line!"""This is really simple stuff, so lets move on. In your program, you often want to add variables into your strings. There are a few ways to do this in python. One way is to use the a comma. In the example below, I will use a loop to demonstrate using a comma to add in a variable.
for i in range(5): #this sets up our for loop print 'this is loop number: ', i
When you run this code you should get:
this is loop number 1
this is loop number 2, etc.
You can also put another string in after the variable as well by using the comma again and the putting in the string.
print 'this is loop number: ', i, ' another string.'
It would then print like this: this is loop number 1 another string.
More variables in strings
Another way to add variables into strings is by using the +. You need to be careful when you use the + because if the variable you are trying to add in is not a string, it won't work. If you have a number, you can
use this to make sure it will work as a string str(variable). This will convert the variable into a string. So, for example:
num = 99 print 'The number is'+str(num) #makes num a string
If you tried to print without the str(), you would get this error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
You can also put another string after the variable just like the comma.
print 'number: '+str(num)+' more text'
If the you are trying to add a variable into the string which is already a string, you don't need the str(), I mainly use it for adding numbers into strings. The other way (and probably the most favored way) of adding a variable into a string is using the % symbol. This way allows you to add variables into strings without closing the string. %s is for strings, %i for integers, %f for floats. See the example below:
string = 'message' integ = 99 floatNum = 88.59 print 'this is a %s, this is an int %i, this is a float %f' %(string, integ, floatNum)
When you print it, you should see all of the variables in the string. If you only have one % variable in the string, you don't need the %() for the variable.
var = 'message' print 'this is a %s' %var
That will print: this is a message. If you want to use the % sign in a string, python can detect that your not trying to put a variable into the string because there will be no variables listed at the end of the string. One other thing which you may find handy is limiting the number of decimal points on a float.
temp = 15.735363873475 print 'The temperature is: %.2f' % temp
It should print only two decimal places. Another thing you may notice is that it will print 15.74 instead of 15.73. This is because python has automatically rounded the number for you. So, lets take what I've talked about today and use it in some code.
print 'program started' #basic string var_a = 'Marry has a little lamb' print 'This is from a nursery rhyme !', var_a #using , for variable var_b = 10 var_c = 51.453 print 'Can you count to '+str(var_B)/>+ '? Because I can!' #using + to add in # a variable and using str() to make it a string for k in range(10):#making a for loop num = k + 1 #computers start counting from 0 so it is now 1 - 10 print 'number %i' %num #using % to put a variable in a string print "I'd like to go to markets, I'd like to go!" #using ' in a string by #changing to " for defining the string print """She moved quietly around the corner, when she saw the number %f on the computer screen. She turned and ran away, that boy had been back to his coding tricks again!""" #printing multiple lines in one print statement print 'That''s all for now!'
In part two, I will talk about formatting strings, joining strings, getting strings from lists and searching strings.
Good luck programming!
If you have any questions, feel free to ask them!
This post has been edited by JackOfAllTrades: 16 October 2010 - 04:44 AM
Reason for edit:: Fixed syntax error