Understanding Strings in Python Programming

undefined
 
Lecture 7
Strings
 
Bryan Burlingame
13 March 2019
 
Announcements
 
Learning Objectives
 
Introduce and discuss strings & sequences
 
Strings
 
A string is a sequence of values, specifically a sequence of characters
Basic string initialization uses quotes, either single ‘’ or double “”
 
 
 
 
Individual letters can be accessed via the [] operator and some index
 
 
 
Remember:  the index starts at zero
 
 
 
len
 
len() returns the number of characters of the string
 
Traversal
 
Traversing a collection is the
process of working on the
collection element by
element
Either a for loop or a while
loop can be used to traverse a
string
The for loops is more natural
 
Slicing a string
 
The slice operator [:] can
extract substrings from a
string
For string[a:b] return the
characters from a to (b-1)
If a is omitted, assume 0
If b is omitted, assume
the end of the string
If both are omitted,
return a copy of the string
 
Mutability
 
Strings are 
immutable
.
The characters of a
string cannot be changed
The “grades[1] = 72” line
passes, the error is on the
next line
 
Mutability
 
Strings are 
immutable
.
The characters of a
string cannot be changed
The “grades[1] = 72” line
passes, the error is on the
next line
Use slices and
concatenation, instead
 
Mutability
 
Strings are 
immutable
.
The characters of a
string cannot be changed
The “grades[1] = 72” line
passes, the error is on the
next line
Use slices and
concatenation, instead
There are mutable
collections, we’ll discuss
some of them soon.
 
Search
 
Search
 is the process of traversing a sequence, evaluating each
member of that sequence for some value, and returning when the
value is found
From the text:
 
Strings Methods
 
Strings have many
methods
Since strings are
immutable, many
methods return a string
Note
: slices of a string are
considered strings as well
 
https://docs.python.org/3/library/stdtypes.html#string-methods
 
String Formatting
 
Strings are generally intended to be consumed by people.  Attractive
and consistent formatting makes this easier
Python has a mini-language just for formatting
Takes the form “Format Spec”.format(thing_to_be_formatted)
 
 
 
 
 
Each element of the format list is matched in order with a 
replacement
field 
{#}
{0} == a, {1} == b, {2} == c, where the format is specified within the {} and
between the {} (note the spaces in the format string and the output)
 
https://docs.python.org/3/library/string.html#formatstrings
 
String Formatting -
Examples
 
Note how the values follow
the replacement field and the
inter-replacement field
characters are added
 
String Formatting - Examples
 
Formatting of the values are controlled in the replacement field
{#:format_spec}
Ex:  {0:.2f} – format the 0
th
 value, display as a float with 2 decimal places
 
String Formatting - General
 
Each of the options on the format_spec line can be included
Print a minimally 10 character wide field, field.  Center the value.  Include
the + sign, show as a 2 decimal point float.  Fill in any white space with an
asterisk *
 
https://docs.python.org/3/library/string.html#formatstrings
 
String Formatting - General
 
Each of the options on the format_spec line can be included
Print a minimally 10 character wide field, field.  Center the value.  Include
the + sign, show as a 2 decimal point float.  Fill in any white space with an
asterisk *
 
 
https://docs.python.org/3/library/string.html#formatstrings
 
String Formatting - General
 
All the values in the format_spec are optional
Go to the docs to understand what each value controls
Note that this is a very succinct mini-language
 
 
https://docs.python.org/3/library/string.html#formatstrings
 
String Formatting - General
 
The format_spec is simply a string; therefore, it can be transformable
 
Resources
 
Downey, A. (2016) 
Think Python, Second Edition
 Sebastopol, CA:  O’Reilly Media
(n.d.). 3.7.0 Documentation. 
String Methods — Python 3.7.0 documentation.
Retrieved October 16, 2018, from
https://docs.python.org/3/library/stdtypes.html#string-methods
Slide Note
Embed
Share

Strings in Python are sequences of characters with various operations like initialization, accessing, traversal, slicing, and mutability. Learn about basic concepts, such as string initialization using quotes, accessing individual characters, determining string length, traversing strings, slicing substrings, and understanding string immutability. Discover how to work with strings effectively in Python programming.


Uploaded on Aug 01, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Lecture 7 Strings Bryan Burlingame 13 March 2019

  2. Homework 5 due up front Homework 6 due next week Begin Arduino next week Read Chapters 9, 10, Automate the Boring Stuff Chapter 7 Announcements

  3. Learning Objectives Introduce and discuss strings & sequences

  4. Strings A string is a sequence of values, specifically a sequence of characters Basic string initialization uses quotes, either single or double Individual letters can be accessed via the [] operator and some index Remember: the index starts at zero

  5. len len() returns the number of characters of the string

  6. Traversal Traversing a collection is the process of working on the collection element by element Either a for loop or a while loop can be used to traverse a string The for loops is more natural

  7. Slicing a string The slice operator [:] can extract substrings from a string For string[a:b] return the characters from a to (b-1) If a is omitted, assume 0 If b is omitted, assume the end of the string If both are omitted, return a copy of the string

  8. Mutability Strings are immutable. The characters of a string cannot be changed The grades[1] = 72 line passes, the error is on the next line

  9. Mutability Strings are immutable. The characters of a string cannot be changed The grades[1] = 72 line passes, the error is on the next line Use slices and concatenation, instead

  10. Mutability Strings are immutable. The characters of a string cannot be changed The grades[1] = 72 line passes, the error is on the next line Use slices and concatenation, instead There are mutable collections, we ll discuss some of them soon.

  11. Search Search is the process of traversing a sequence, evaluating each member of that sequence for some value, and returning when the value is found From the text:

  12. Strings Methods Strings have many methods Since strings are immutable, many methods return a string Note: slices of a string are considered strings as well https://docs.python.org/3/library/stdtypes.html#string-methods

  13. String Formatting Strings are generally intended to be consumed by people. Attractive and consistent formatting makes this easier Python has a mini-language just for formatting Takes the form Format Spec .format(thing_to_be_formatted) Each element of the format list is matched in order with a replacement field {#} {0} == a, {1} == b, {2} == c, where the format is specified within the {} and between the {} (note the spaces in the format string and the output) https://docs.python.org/3/library/string.html#formatstrings

  14. String Formatting - Examples Note how the values follow the replacement field and the inter-replacement field characters are added

  15. String Formatting - Examples Formatting of the values are controlled in the replacement field {#:format_spec} Ex: {0:.2f} format the 0th value, display as a float with 2 decimal places

  16. String Formatting - General Each of the options on the format_spec line can be included Print a minimally 10 character wide field, field. Center the value. Include the + sign, show as a 2 decimal point float. Fill in any white space with an asterisk * https://docs.python.org/3/library/string.html#formatstrings

  17. String Formatting - General Each of the options on the format_spec line can be included Print a minimally 10 character wide field, field. Center the value. Include the + sign, show as a 2 decimal point float. Fill in any white space with an asterisk * https://docs.python.org/3/library/string.html#formatstrings

  18. String Formatting - General All the values in the format_spec are optional Go to the docs to understand what each value controls Note that this is a very succinct mini-language https://docs.python.org/3/library/string.html#formatstrings

  19. String Formatting - General The format_spec is simply a string; therefore, it can be transformable

  20. Resources Downey, A. (2016) Think Python, Second EditionSebastopol, CA: O Reilly Media (n.d.). 3.7.0 Documentation. String Methods Python 3.7.0 documentation. Retrieved October 16, 2018, from https://docs.python.org/3/library/stdtypes.html#string-methods

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#