Python Strings – A rough guide

Anything stored in single or double quotes is seen as a string in Python, lets see some examples:

print "I am a string"
print 'I am a string'

Both result in

I am a string

We can construct strings using variables by adding + to patch them together:

name = 'John'
surname = "Player"
print "Hi, my first name is " + name + ' and my surname is ' + surname

Would output:

Hi, my first name is John and my surname is Player

Although mixing single and double quotes is a dickmove! Stick with doubles, it helps readability.

To confirm that the above is actually a string, you can use printand typetogether:

print type("Hi, my first name is " + name + ' and my surname is ' + surname)

The output will be:

<type 'str'>

Confirming that the data type is in fact a string.