On multiline strings: I consider this a pretty wrong and misleading example. I expectet a string with multiple lines, not multiple lines that create a single-line string, which has no line-break!
Make multiline string more clear with two distinct examples.
|Deletions are marked like this.||Additions are marked like this.|
|Line 8:||Line 8:|
|Line 16:||Line 14:|
|Line 24:||Line 20:|
|Line 43:||Line 37:|
|Line 54:||Line 46:|
|Line 62:||Line 52:|
|Line 72:||Line 60:|
|Line 83:||Line 69:|
|Line 88:||Line 72:|
|string=('This is a '
|string = '''This is a string with embedded newlines.
Also known as a tripled-quoted string.
Whitespace at the beginning of lines is included,
so the above line is indented but the others are not.
|Line 91:||Line 78:|
|Defining long strings over multiple lines|
|Line 92:||Line 80:|
string = ('This is a single long, long string'
' written over many lines for convenience'
' using implicit concatenation to join each'
' piece into a single string without extra'
' newlines (unless you add them yourself).')
Here are some samples to help get a better idea of Python's syntax:
Hello World (the traditional first program)
print 'Hello world!'
name = 'Monty' print 'Hello, %s' % name
Defining a function
def add_one(x): return x + 1
Testing variable equality
x=1 y=2 print 'x is equal to y: %s' % (x==y) z=1 print 'x is equal to z: %s' % (x==z) names=['Donald','Jake','Phil'] words=['Random','Words','Dogs'] if names==words: print 'Names list is equal to words' else: print 'Names list isn\'t equal to words' new_names=['Donald','Jake','Phil'] print 'New names list is equal to names: %s' % (new_names==names)
Defining a class with two methods
class Talker(object): def greet(self, name): print 'Hello, %s!' % name def farewell(self, name): print 'Farewell, %s!' % name
Defining a list
dynamic_languages = ['Python', 'Ruby', 'Groovy'] dynamic_languages.append('Lisp')
Defining a dictionary
numbered_words=dict() numbered_words='world' numbered_words='Hello' numbered_words='!'
Defining a while loop
while True: if value==wanted_value: break else: pass
Defining multiline strings
string = '''This is a string with embedded newlines. Also known as a tripled-quoted string. Whitespace at the beginning of lines is included, so the above line is indented but the others are not. '''
Defining long strings over multiple lines
string = ('This is a single long, long string' ' written over many lines for convenience' ' using implicit concatenation to join each' ' piece into a single string without extra' ' newlines (unless you add them yourself).')
Defining a for loop
for x in xrange(1,4): print ('Hello, new Python user!' 'This is time number %d') % (x)