SlideShare uma empresa Scribd logo
1 de 61
Baixar para ler offline
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.

Mais conteúdo relacionado

Mais procurados

Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Qiangning Hong
 
F# delight
F# delightF# delight
F# delightpriort
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administrationvceder
 
What Shazam doesn't want you to know
What Shazam doesn't want you to knowWhat Shazam doesn't want you to know
What Shazam doesn't want you to knowRoy van Rijn
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)teach4uin
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line toolsEric Wilson
 
JFugue, Music, and the Future of Java [JavaOne 2016, CON1851]
JFugue, Music, and the Future of Java [JavaOne 2016, CON1851]JFugue, Music, and the Future of Java [JavaOne 2016, CON1851]
JFugue, Music, and the Future of Java [JavaOne 2016, CON1851]David Koelle
 
The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...
The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...
The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...David Koelle
 
Basic NLP with Python and NLTK
Basic NLP with Python and NLTKBasic NLP with Python and NLTK
Basic NLP with Python and NLTKFrancesco Bruni
 
Τα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonΤα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonMoses Boudourides
 
Python 3000
Python 3000Python 3000
Python 3000Bob Chao
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awkYogesh Sawant
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 

Mais procurados (20)

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

Semelhante a Strings (20)

unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
strings11.pdf
strings11.pdfstrings11.pdf
strings11.pdf
 
Python- strings
Python- stringsPython- strings
Python- strings
 
UNIT 4 python.pptx
UNIT 4 python.pptxUNIT 4 python.pptx
UNIT 4 python.pptx
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python Basic
Python BasicPython Basic
Python Basic
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
P3 2018 python_regexes
P3 2018 python_regexesP3 2018 python_regexes
P3 2018 python_regexes
 
Python data handling
Python data handlingPython data handling
Python data handling
 
python_strings.pdf
python_strings.pdfpython_strings.pdf
python_strings.pdf
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Python ppt
Python pptPython ppt
Python ppt
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
php_string.pdf
php_string.pdfphp_string.pdf
php_string.pdf
 
PPS_Unit 4.ppt
PPS_Unit 4.pptPPS_Unit 4.ppt
PPS_Unit 4.ppt
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)
 

Último

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

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.