SlideShare a Scribd company logo
Python 
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.
Strings are sequences of characters 
Python Strings
Strings are sequences of characters 
No separate character type: just a string of length 1 
Python Strings
Strings are sequences of characters 
No separate character type: just a string of length 1 
Indexed exactly like lists 
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 
Python Strings
for iterates through characters 
Python Strings
for iterates through characters 
name = 'Darwin' 
for c in name: 
print c 
Darwin 
Python Strings
Use either ' or " (as long as they match) 
Python Strings
Use either ' or " (as long as they match) 
print 'Alan', "Turing" 
Alan Turing 
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 
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 
Python Strings
Strings are compared character by character 
from left to right 
Python Strings
Strings are compared character by character 
from left to right 
print 'a' < 'b' 
True 
Python Strings
Strings are compared character by character 
from left to right 
print 'a' < 'b' 
True 
print 'ab' < 'abc' 
True 
Python Strings
Strings are compared character by character 
from left to right 
print 'a' < 'b' 
True 
print 'ab' < 'abc' 
True 
print '1' < '9' 
True 
Python Strings
Strings are compared character by character 
from left to right 
print 'a' < 'b' 
True 
print 'ab' < 'abc' 
True 
print '1' < '9' 
True 
print '100' < '9' 
True 
Python Strings
Strings are compared character by character 
from left to right 
print 'a' < 'b' 
True 
print 'ab' < 'abc' 
True 
print '1' < '9' 
True 
print '100' < '9' 
True 
print 'A' < 'a' 
True 
Python Strings
Strings are immutable : cannot be changed in place 
Python Strings
Strings are immutable : cannot be changed in place 
name = 'Darwin' 
name[0] = 'C' 
TypeError: 'str' object does not support item assignment 
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 
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 
Python Strings
Use + to concatenate strings 
Python Strings
Use + to concatenate strings 
name = 'Charles' + ' ' + 'Darwin' 
print name 
Charles Darwin 
Python Strings
Use + to concatenate strings 
name = 'Charles' + ' ' + 'Darwin' 
print name 
Charles Darwin 
Concatenation always produces a new string 
Python Strings
Use + to concatenate strings 
name = 'Charles' + ' ' + 'Darwin' 
print name 
Charles Darwin 
Concatenation always produces a new string 
original = 'Charles' original 'Charles' 
Python Strings
Use + to concatenate strings 
name = 'Charles' + ' ' + 'Darwin' 
print name 
Charles Darwin 
Concatenation always produces a new string 
'Charles' 
original = 'Charles' original 
name = original 
name 
Python Strings
Use + to concatenate strings 
name = 'Charles' + ' ' + 'Darwin' 
print name 
Charles Darwin 
Concatenation always produces a new string 
original = 'Charles' 
name = original 
name += ' Darwin' 
'Charles' 
original 
name 'Charles Darwin' 
Python Strings
Often used to format output 
Python Strings
Often used to format output 
print 'reagant: ' + str(reagant_id) + ' produced ' + str(percentage_yield) + '% yield' 
Python Strings
Often used to format output 
print 'reagant: ' + str(reagant_id) + ' produced ' + str(percentage_yield) + '% yield' 
There's a better way... 
Python Strings
Use string % value to format output 
Python Strings
Use string % value to format output 
output = 'reagant: %d' % 123 
print output 
reagant: 123 
Python Strings
Use string % value to format output 
output = 'reagant: %d' % 123 
print output 
reagant: 123 
percentage_yield = 12.3 
print 'yield: %6.2f' % percentage_yield 
yield: 12.30 
Python Strings
And string % (v1, v2, ...) for multiple values 
Python Strings
And string % (v1, v2, ...) for multiple values 
reagant_id = 123 
percentage_yield = 12.3 
print 'reagant: %d produced %f%% yield' %  
(reagant_id, percentage_yield) 
reagant: 123 produced 12.30% yield 
Python Strings
And string % (v1, v2, ...) for multiple values 
reagant_id = 123 
percentage_yield = 12.3 
print 'reagant: %d produced %f%% yield' %  
(reagant_id, percentage_yield) 
reagant: 123 produced 12.30% yield 
% operator turns double '%%' into single '%' 
Python Strings
Use n to represent a newline character 
Python Strings
Use n to represent a newline character 
Use ' for single quote, " for double quote 
Python Strings
Use n to represent a newline character 
Use ' for single quote, " for double quote 
print 'There isn't timento do it right.' 
There isn't time 
to do it right. 
Python Strings
Use n to represent a newline character 
Use ' for single quote, " for double quote 
print 'There isn't timento do it right.' 
There isn't time 
to 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." 
Python Strings
Use  for a literal  character 
Python Strings
Use  for a literal  character 
print 'Most mathematicians write ab instead of a%b.' 
Most mathematicians write ab instead of a%b. 
Python Strings
Use  for a literal  character 
print 'Most mathematicians write ab instead of a%b.' 
Most mathematicians write ab instead of a%b. 
Common pattern with escape sequences 
Python Strings
Use  for a literal  character 
print 'Most mathematicians write ab instead of a%b.' 
Most mathematicians write ab instead of a%b. 
Common pattern with escape sequences 
– Use a character to mean "what follows is special" 
Python Strings
Use  for a literal  character 
print 'Most mathematicians write ab instead of a%b.' 
Most mathematicians write ab instead of a%b. 
Common pattern with escape sequences 
– Use a character to mean "what follows is special" 
– Double it up to mean "that character itself" 
Python Strings
Use triple quotes (either kind) for multi-line strings 
Python Strings
Use triple quotes (either kind) for multi-line strings 
quote = '''We can only see 
a short distance ahead, 
but we can see plenty there 
that needs to be done.''' 
Python Strings
Use triple quotes (either kind) for multi-line strings 
quote = '''We can only see 
a short distance ahead, 
but we can see plenty there 
that needs to be done.''' 
d , n b u 
Python Strings
Use triple quotes (either kind) for multi-line strings 
quote = '''We can only see 
a short distance ahead, 
but we can see plenty there 
that needs to be done.''' 
quote = 'We can only seena short distance aheadn' 'but we can see plenty therenthat needs to be done.' 
Python Strings
Strings have methods 
Python Strings
Strings have methods 
name = 'newTON' 
print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON 
Python Strings
Strings have methods 
name = 'newTON' 
print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON 
dna = 'acggtggtcac' 
print dna.count('g'), dna.count('x') 
4 0 
Python Strings
Strings have methods 
name = 'newTON' 
print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON 
dna = 'acggtggtcac' 
print dna.count('g'), dna.count('x') 
4 0 
print dna.find('t'), dna.find('t', 5), dna.find('x') 
4 7 -1 
Python Strings
Strings have methods 
name = 'newTON' 
print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON 
dna = 'acggtggtcac' 
print dna.count('g'), dna.count('x') 
4 0 
print dna.find('t'), dna.find('t', 5), dna.find('x') 
4 7 -1 
print dna.replace('t', 'x'), dna 
acggxggxcac acggtggtcac 
Python Strings
Strings have methods 
name = 'newTON' 
print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON 
dna = 'acggtggtcac' 
print dna.count('g'), dna.count('x') 
4 0 
print dna.find('t'), dna.find('t', 5), dna.find('x') 
4 7 -1 
print dna.replace('t', 'x') 
acggxggxcac acggtggtcac 
print dna.replace('gt', '') 
acggcac 
Python Strings
Can chain method calls together 
Python Strings
Can chain method calls together 
element = 'cesium' 
print element.upper().center(10, '.') 
Python Strings
Can chain method calls together 
element = 'cesium' 
print element.upper().center(10, '.') 
convert to upper case 
Python Strings
Can chain method calls together 
element = 'cesium' 
print element.upper().center(10, '.') 
center in a field 
10 characters wide 
Python Strings
Can chain method calls together 
element = 'cesium' 
print element.upper().center(10, '.') 
..CESIUM.. 
Python Strings
narrated by 
Dominique Vuvan 
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.

More Related Content

What's hot (20)

ODP
Programming Under Linux In Python
PDF
Python于Web 2.0网站的应用 - QCon Beijing 2010
PDF
F# delight
PDF
Python for Linux System Administration
ODP
What Shazam doesn't want you to know
PDF
PPTX
Python Workshop
PPTX
L14 string handling(string buffer class)
PDF
Geeks Anonymes - Le langage Go
PPT
Unix command-line tools
PPTX
JFugue, Music, and the Future of Java [JavaOne 2016, CON1851]
PPTX
The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...
PDF
Music as data
PDF
Grep Introduction
PDF
Basic NLP with Python and NLTK
PDF
Τα Πολύ Βασικά για την Python
PPTX
ODP
Python 3000
PPT
Learning sed and awk
PPTX
131 Lab slides (all in one)
Programming Under Linux In Python
Python于Web 2.0网站的应用 - QCon Beijing 2010
F# delight
Python for Linux System Administration
What Shazam doesn't want you to know
Python Workshop
L14 string handling(string buffer class)
Geeks Anonymes - Le langage Go
Unix command-line tools
JFugue, Music, and the Future of Java [JavaOne 2016, CON1851]
The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...
Music as data
Grep Introduction
Basic NLP with Python and NLTK
Τα Πολύ Βασικά για την Python
Python 3000
Learning sed and awk
131 Lab slides (all in one)
Ad

Similar to Strings (20)

PPTX
unit-4 regular expression.pptx
PDF
Python tutorial
PPTX
Day5 String python language for btech.pptx
PDF
stringsinpython-181122100212.pdf
PPTX
Python Workshop - Learn Python the Hard Way
PDF
strings11.pdf
PDF
Python- strings
PPTX
UNIT 4 python.pptx
PDF
Strings in python
PDF
Python Basic
PPTX
Chapter 11 Strings and methods [Autosaved].pptx
PDF
Python programming : Strings
PPTX
Python Strings.pptx
PPTX
P3 2018 python_regexes
PDF
Python data handling
PDF
python_strings.pdf
PPTX
Strings.pptxbfffffffffffffffffffffffffffffffffffffffffd
PDF
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
PPTX
stringggg.pptxtujd7ttttttttttttttttttttttttttttttttttt
PDF
Strings in Python
unit-4 regular expression.pptx
Python tutorial
Day5 String python language for btech.pptx
stringsinpython-181122100212.pdf
Python Workshop - Learn Python the Hard Way
strings11.pdf
Python- strings
UNIT 4 python.pptx
Strings in python
Python Basic
Chapter 11 Strings and methods [Autosaved].pptx
Python programming : Strings
Python Strings.pptx
P3 2018 python_regexes
Python data handling
python_strings.pdf
Strings.pptxbfffffffffffffffffffffffffffffffffffffffffd
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
stringggg.pptxtujd7ttttttttttttttttttttttttttttttttttt
Strings in Python
Ad

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
A Presentation on Artificial Intelligence
PDF
Approach and Philosophy of On baking technology
PPT
Teaching material agriculture food technology
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Cloud computing and distributed systems.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Assigned Numbers - 2025 - Bluetooth® Document
A Presentation on Artificial Intelligence
Approach and Philosophy of On baking technology
Teaching material agriculture food technology
A comparative analysis of optical character recognition models for extracting...
MIND Revenue Release Quarter 2 2025 Press Release
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Per capita expenditure prediction using model stacking based on satellite ima...
Spectral efficient network and resource selection model in 5G networks
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf

Strings

  • 1. Python 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.
  • 2. Strings are sequences of characters Python Strings
  • 3. Strings are sequences of characters No separate character type: just a string of length 1 Python Strings
  • 4. Strings are sequences of characters No separate character type: just a string of length 1 Indexed exactly like lists Python Strings
  • 5. 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 Python Strings
  • 6. for iterates through characters Python Strings
  • 7. for iterates through characters name = 'Darwin' for c in name: print c Darwin Python Strings
  • 8. Use either ' or " (as long as they match) Python Strings
  • 9. Use either ' or " (as long as they match) print 'Alan', "Turing" Alan Turing Python Strings
  • 10. Use either ' or " (as long as they match) print 'Alan', "Turing" Alan Turing Strings are the same no matter how they're created Python Strings
  • 11. 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 Python Strings
  • 12. Strings are compared character by character from left to right Python Strings
  • 13. Strings are compared character by character from left to right print 'a' < 'b' True Python Strings
  • 14. Strings are compared character by character from left to right print 'a' < 'b' True print 'ab' < 'abc' True Python Strings
  • 15. Strings are compared character by character from left to right print 'a' < 'b' True print 'ab' < 'abc' True print '1' < '9' True Python Strings
  • 16. Strings are compared character by character from left to right print 'a' < 'b' True print 'ab' < 'abc' True print '1' < '9' True print '100' < '9' True Python Strings
  • 17. Strings are compared character by character from left to right print 'a' < 'b' True print 'ab' < 'abc' True print '1' < '9' True print '100' < '9' True print 'A' < 'a' True Python Strings
  • 18. Strings are immutable : cannot be changed in place Python Strings
  • 19. Strings are immutable : cannot be changed in place name = 'Darwin' name[0] = 'C' TypeError: 'str' object does not support item assignment Python Strings
  • 20. Strings are immutable : cannot be changed in place name = 'Darwin' name[0] = 'C' TypeError: 'str' object does not support item assignment Immutability improves performance Python Strings
  • 21. 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 Python Strings
  • 22. Use + to concatenate strings Python Strings
  • 23. Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Python Strings
  • 24. Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Concatenation always produces a new string Python Strings
  • 25. Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Concatenation always produces a new string original = 'Charles' original 'Charles' Python Strings
  • 26. Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Concatenation always produces a new string 'Charles' original = 'Charles' original name = original name Python Strings
  • 27. Use + to concatenate strings name = 'Charles' + ' ' + 'Darwin' print name Charles Darwin Concatenation always produces a new string original = 'Charles' name = original name += ' Darwin' 'Charles' original name 'Charles Darwin' Python Strings
  • 28. Often used to format output Python Strings
  • 29. Often used to format output print 'reagant: ' + str(reagant_id) + ' produced ' + str(percentage_yield) + '% yield' Python Strings
  • 30. Often used to format output print 'reagant: ' + str(reagant_id) + ' produced ' + str(percentage_yield) + '% yield' There's a better way... Python Strings
  • 31. Use string % value to format output Python Strings
  • 32. Use string % value to format output output = 'reagant: %d' % 123 print output reagant: 123 Python Strings
  • 33. Use string % value to format output output = 'reagant: %d' % 123 print output reagant: 123 percentage_yield = 12.3 print 'yield: %6.2f' % percentage_yield yield: 12.30 Python Strings
  • 34. And string % (v1, v2, ...) for multiple values Python Strings
  • 35. And string % (v1, v2, ...) for multiple values reagant_id = 123 percentage_yield = 12.3 print 'reagant: %d produced %f%% yield' % (reagant_id, percentage_yield) reagant: 123 produced 12.30% yield Python Strings
  • 36. And string % (v1, v2, ...) for multiple values reagant_id = 123 percentage_yield = 12.3 print 'reagant: %d produced %f%% yield' % (reagant_id, percentage_yield) reagant: 123 produced 12.30% yield % operator turns double '%%' into single '%' Python Strings
  • 37. Use n to represent a newline character Python Strings
  • 38. Use n to represent a newline character Use ' for single quote, " for double quote Python Strings
  • 39. Use n to represent a newline character Use ' for single quote, " for double quote print 'There isn't timento do it right.' There isn't time to do it right. Python Strings
  • 40. Use n to represent a newline character Use ' for single quote, " for double quote print 'There isn't timento do it right.' There isn't time to 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." Python Strings
  • 41. Use for a literal character Python Strings
  • 42. Use for a literal character print 'Most mathematicians write ab instead of a%b.' Most mathematicians write ab instead of a%b. Python Strings
  • 43. Use for a literal character print 'Most mathematicians write ab instead of a%b.' Most mathematicians write ab instead of a%b. Common pattern with escape sequences Python Strings
  • 44. Use for a literal character print 'Most mathematicians write ab instead of a%b.' Most mathematicians write ab instead of a%b. Common pattern with escape sequences – Use a character to mean "what follows is special" Python Strings
  • 45. Use for a literal character print 'Most mathematicians write ab instead of a%b.' Most mathematicians write ab instead of a%b. Common pattern with escape sequences – Use a character to mean "what follows is special" – Double it up to mean "that character itself" Python Strings
  • 46. Use triple quotes (either kind) for multi-line strings Python Strings
  • 47. Use triple quotes (either kind) for multi-line strings quote = '''We can only see a short distance ahead, but we can see plenty there that needs to be done.''' Python Strings
  • 48. Use triple quotes (either kind) for multi-line strings quote = '''We can only see a short distance ahead, but we can see plenty there that needs to be done.''' d , n b u Python Strings
  • 49. Use triple quotes (either kind) for multi-line strings quote = '''We can only see a short distance ahead, but we can see plenty there that needs to be done.''' quote = 'We can only seena short distance aheadn' 'but we can see plenty therenthat needs to be done.' Python Strings
  • 50. Strings have methods Python Strings
  • 51. Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON Python Strings
  • 52. Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON dna = 'acggtggtcac' print dna.count('g'), dna.count('x') 4 0 Python Strings
  • 53. Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON dna = 'acggtggtcac' print dna.count('g'), dna.count('x') 4 0 print dna.find('t'), dna.find('t', 5), dna.find('x') 4 7 -1 Python Strings
  • 54. Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON dna = 'acggtggtcac' print dna.count('g'), dna.count('x') 4 0 print dna.find('t'), dna.find('t', 5), dna.find('x') 4 7 -1 print dna.replace('t', 'x'), dna acggxggxcac acggtggtcac Python Strings
  • 55. Strings have methods name = 'newTON' print name.capitalize(), name.upper(), name.lower(), Newton NEWTON newton newTON dna = 'acggtggtcac' print dna.count('g'), dna.count('x') 4 0 print dna.find('t'), dna.find('t', 5), dna.find('x') 4 7 -1 print dna.replace('t', 'x') acggxggxcac acggtggtcac print dna.replace('gt', '') acggcac Python Strings
  • 56. Can chain method calls together Python Strings
  • 57. Can chain method calls together element = 'cesium' print element.upper().center(10, '.') Python Strings
  • 58. Can chain method calls together element = 'cesium' print element.upper().center(10, '.') convert to upper case Python Strings
  • 59. Can chain method calls together element = 'cesium' print element.upper().center(10, '.') center in a field 10 characters wide Python Strings
  • 60. Can chain method calls together element = 'cesium' print element.upper().center(10, '.') ..CESIUM.. Python Strings
  • 61. narrated by Dominique Vuvan 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.