Strings

61
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Python

description

Python - Strings

Transcript of Strings

Page 1: Strings

Strings

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Python

Page 2: Strings

Python Strings

Strings are sequences of characters

Page 3: Strings

Python Strings

Strings are sequences of characters

No separate character type: just a string of length 1

Page 4: Strings

Python Strings

Strings are sequences of characters

No separate character type: just a string of length 1

Indexed exactly like lists

Page 5: Strings

Python Strings

Strings are sequences of characters

No separate character type: just a string of length 1

Indexed exactly like lists

name = 'Darwin'print name[0], name[-1]D n

Page 6: Strings

Python Strings

for iterates through characters

Page 7: Strings

Python Strings

for iterates through characters

name = 'Darwin'for c in name: print cDarwin

Page 8: Strings

Python Strings

Use either ' or " (as long as they match)

Page 9: Strings

Python Strings

Use either ' or " (as long as they match)

print 'Alan', "Turing"Alan Turing

Page 10: Strings

Python Strings

Use either ' or " (as long as they match)

print 'Alan', "Turing"Alan Turing

Strings are the same no matter how they're created

Page 11: Strings

Python Strings

Use either ' or " (as long as they match)

print 'Alan', "Turing"Alan Turing

Strings are the same no matter how they're created

print 'Alan' == "Alan"True

Page 12: Strings

Python Strings

Strings are compared character by character

from left to right

Page 13: Strings

Python Strings

Strings are compared character by character

from left to right

print 'a' < 'b'True

Page 14: Strings

Python Strings

Strings are compared character by character

from left to right

print 'a' < 'b'Trueprint 'ab' < 'abc'True

Page 15: Strings

Python Strings

Strings are compared character by character

from left to right

print 'a' < 'b'Trueprint 'ab' < 'abc'Trueprint '1' < '9'True

Page 16: Strings

Python Strings

Strings are compared character by character

from left to right

print 'a' < 'b'Trueprint 'ab' < 'abc'Trueprint '1' < '9'Trueprint '100' < '9'True

Page 17: Strings

Python Strings

Strings are compared character by character

from left to right

print 'a' < 'b'Trueprint 'ab' < 'abc'Trueprint '1' < '9'Trueprint '100' < '9'Trueprint 'A' < 'a'True

Page 18: Strings

Python Strings

Strings are immutable : cannot be changed in place

Page 19: Strings

Python Strings

Strings are immutable : cannot be changed in place

name = 'Darwin'name[0] = 'C'TypeError: 'str' object does not support item assignment

Page 20: Strings

Python Strings

Strings are immutable : cannot be changed in place

name = 'Darwin'name[0] = 'C'TypeError: 'str' object does not support item assignment

Immutability improves performance

Page 21: Strings

Python Strings

Strings are immutable : cannot be changed in place

name = 'Darwin'name[0] = 'C'TypeError: 'str' object does not support item assignment

Immutability improves performance

See later how immutability improves programmers'

performance

Page 22: Strings

Python Strings

Use + to concatenate strings

Page 23: Strings

Python Strings

Use + to concatenate strings

name = 'Charles' + ' ' + 'Darwin'print nameCharles Darwin

Page 24: Strings

Python Strings

Use + to concatenate strings

name = 'Charles' + ' ' + 'Darwin'print nameCharles Darwin

Concatenation always produces a new string

Page 25: Strings

Python Strings

Use + to concatenate strings

name = 'Charles' + ' ' + 'Darwin'print nameCharles Darwin

Concatenation always produces a new string

'Charles'originaloriginal = 'Charles'

Page 26: Strings

Python Strings

Use + to concatenate strings

name = 'Charles' + ' ' + 'Darwin'print nameCharles Darwin

Concatenation always produces a new string

'Charles'

name

originaloriginal = 'Charles'name = original

Page 27: Strings

Python Strings

Use + to concatenate strings

name = 'Charles' + ' ' + 'Darwin'print nameCharles Darwin

Concatenation always produces a new string

original = 'Charles'name = originalname += ' Darwin'

'Charles'

'Charles Darwin'name

original

Page 28: Strings

Python Strings

Often used to format output

Page 29: Strings

Python Strings

Often used to format output

print 'reagant: ' + str(reagant_id) + ' produced ' + \ str(percentage_yield) + '% yield'

Page 30: Strings

Python Strings

Often used to format output

print 'reagant: ' + str(reagant_id) + ' produced ' + \ str(percentage_yield) + '% yield'

There's a better way...

Page 31: Strings

Python Strings

Use string % value to format output

Page 32: Strings

Python Strings

Use string % value to format output

output = 'reagant: %d' % 123print outputreagant: 123

Page 33: Strings

Python Strings

Use string % value to format output

output = 'reagant: %d' % 123print outputreagant: 123

percentage_yield = 12.3print 'yield: %6.2f' % percentage_yieldyield: 12.30

Page 34: Strings

Python Strings

And string % (v1, v2, ...) for multiple values

Page 35: Strings

Python Strings

And string % (v1, v2, ...) for multiple values

reagant_id = 123percentage_yield = 12.3print 'reagant: %d produced %f%% yield' % \ (reagant_id, percentage_yield)reagant: 123 produced 12.30% yield

Page 36: Strings

Python Strings

And string % (v1, v2, ...) for multiple values

reagant_id = 123percentage_yield = 12.3print 'reagant: %d produced %f%% yield' % \ (reagant_id, percentage_yield)reagant: 123 produced 12.30% yield

% operator turns double '%%' into single '%'

Page 37: Strings

Python Strings

Use \n to represent a newline character

Page 38: Strings

Python Strings

Use \n to represent a newline character

Use \' for single quote, \" for double quote

Page 39: Strings

Python Strings

Use \n to represent a newline character

Use \' for single quote, \" for double quote

print 'There isn\'t time\nto do it right.'There isn't timeto do it right.

Page 40: Strings

Python Strings

Use \n to represent a newline character

Use \' for single quote, \" for double quote

print 'There isn\'t time\nto do it right.'There isn't timeto do it right.

print "But you said,\n\"There is time to do it over.\""But you said,"There is time to do it over."

Page 41: Strings

Python Strings

Use \\ for a literal \ character

Page 42: Strings

Python Strings

print 'Most mathematicians write a\\b instead of a%b.'Most mathematicians write a\b instead of a%b.

Use \\ for a literal \ character

Page 43: Strings

Python Strings

print 'Most mathematicians write a\\b instead of a%b.'Most mathematicians write a\b instead of a%b.

Use \\ for a literal \ character

Common pattern with escape sequences

Page 44: Strings

Python Strings

print 'Most mathematicians write a\\b instead of a%b.'Most mathematicians write a\b instead of a%b.

Use \\ for a literal \ character

Common pattern with escape sequences

– Use a character to mean "what follows is special"

Page 45: Strings

Python Strings

print 'Most mathematicians write a\\b instead of a%b.'Most mathematicians write a\b instead of a%b.

Use \\ for a literal \ character

Common pattern with escape sequences

– Use a character to mean "what follows is special"

– Double it up to mean "that character itself"

Page 46: Strings

Python Strings

Use triple quotes (either kind) for multi-line strings

Page 47: Strings

Python Strings

Use triple quotes (either kind) for multi-line strings

quote = '''We can only seea short distance ahead,but we can see plenty therethat needs to be done.'''

Page 48: Strings

Python Strings

quote = '''We can only seea short distance ahead,but we can see plenty therethat needs to be done.'''

d , \n b u

Use triple quotes (either kind) for multi-line strings

Page 49: Strings

Python Strings

quote = '''We can only seea short distance ahead,but we can see plenty therethat needs to be done.'''

quote = 'We can only see\na short distance ahead\n' + \ 'but we can see plenty there\nthat needs to be done.'

Use triple quotes (either kind) for multi-line strings

Page 50: Strings

Python Strings

Strings have methods

Page 51: Strings

Python Strings

Strings have methods

name = 'newTON'print name.capitalize(), name.upper(), name.lower(), nameNewton NEWTON newton newTON

Page 52: Strings

Python Strings

Strings have methods

name = 'newTON'print name.capitalize(), name.upper(), name.lower(), nameNewton NEWTON newton newTONdna = 'acggtggtcac'print dna.count('g'), dna.count('x')4 0

Page 53: Strings

Python Strings

Strings have methods

name = 'newTON'print name.capitalize(), name.upper(), name.lower(), nameNewton NEWTON newton newTONdna = 'acggtggtcac'print dna.count('g'), dna.count('x')4 0print dna.find('t'), dna.find('t', 5), dna.find('x')4 7 -1

Page 54: Strings

Python Strings

Strings have methods

name = 'newTON'print name.capitalize(), name.upper(), name.lower(), nameNewton NEWTON newton newTONdna = 'acggtggtcac'print dna.count('g'), dna.count('x')4 0print dna.find('t'), dna.find('t', 5), dna.find('x')4 7 -1print dna.replace('t', 'x'), dnaacggxggxcac acggtggtcac

Page 55: Strings

Python Strings

Strings have methods

name = 'newTON'print name.capitalize(), name.upper(), name.lower(), nameNewton NEWTON newton newTONdna = 'acggtggtcac'print dna.count('g'), dna.count('x')4 0print dna.find('t'), dna.find('t', 5), dna.find('x')4 7 -1print dna.replace('t', 'x')acggxggxcac acggtggtcacprint dna.replace('gt', '')acggcac

Page 56: Strings

Python Strings

Can chain method calls together

Page 57: Strings

Python Strings

Can chain method calls together

element = 'cesium'print element.upper().center(10, '.')

Page 58: Strings

Python Strings

Can chain method calls together

element = 'cesium'print element.upper().center(10, '.')

convert to upper case

Page 59: Strings

Python Strings

Can chain method calls together

element = 'cesium'print element.upper().center(10, '.')

center in a field

10 characters wide

Page 60: Strings

Python Strings

Can chain method calls together

element = 'cesium'print element.upper().center(10, '.')..CESIUM..

Page 61: Strings

October 2010

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

narrated by

Dominique Vuvan