Lesson 4 Strings
Pragmatic AI Labs
This notebook was produced by Pragmatic AI Labs. You can continue learning about these topics by:
- Buying a copy of Pragmatic AI: An Introduction to Cloud-Based Machine Learning
- Reading an online copy of Pragmatic AI:Pragmatic AI: An Introduction to Cloud-Based Machine Learning
- Watching video Essential Machine Learning and AI with Python and Jupyter Notebook-Video-SafariOnline on Safari Books Online.
- Watching video AWS Certified Machine Learning-Speciality
- Purchasing video Essential Machine Learning and AI with Python and Jupyter Notebook- Purchase Video
- Viewing more content at noahgift.com
4.1 Use string methods
String Quoting
Single quotes
'Here is a string'
Double quotes
True
Triple Strings
Wikipedia is hosted by the Wikimedia Foundation,
a non-profit organization that also hosts a range of other projects.
Raw Strings
...wrote on twitter he is the greatest "heavyw8e!
fighter of all time
...wrote on twitter he is the greatest "heavyw8e! \nfighter of all time
Case Manipulation
'Patrick Tayluer'
'Patrick tayluer'
'patrick tayluer'
'PATRICK TAYLUER'
'pATRICK tAYLUER'
'Patrick Tayluer'
Interrogation
11
4
8
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-67-fcd85454de2b> in <module>()
----> 1 river.index('r')
ValueError: substring not found
-1
True
True
True
Content Type
False
True
True
False
True
False
More information: String Methods
4.2 Format strings
F-strings where introduced in Python 3.6. They prefixed by either a ‘F’ or ‘f’ before the beginning quotation mark. Values can be inserted into F-strings at runtime using replacement fields which are deliminated by curly braces.
Insert variable into replacement field
"Noam Pikelny's banjo has 5 strings and 24 frets"
Insert expression into replacement field
'12 times 32 equals 384'
Index list in string replacement fields
'Performances will be held by Bill Evans, Tony Trischka, and Alan Munde'
Conversion flags
A conversion flag can be specified to convert the type of the value before formatting. The three available flags are ‘s’, ‘r’ and ‘a’.
Using str conversion
'Calling str() on a the list [1, 2, 3, 4, 5] produces [1, 2, 3, 4, 5]'
Using repr conversiont
"Calling repr on the string pistacio results in 'pistacio'"
Using ascii conversion
"The ascii version of √ is '\\u221a'"
Padding a number
'To pad the number 13 to 5 places: 13'
Setting padding value at runtime
'To pad the number 13 to 5 places: 13'
More information: Format String Syntax
Other String Formatting: String Format Method
4.3 Manipulate strings
Concatenation
'Bobberoo'
'ABABABABABABABAB'
Remove Whitespace
' The Yankee Clipper '
'The Yankee Clipper'
'The Yankee Clipper '
' The Yankee Clipper'
' The Yankee Cli'
Add padding
'***Boston***'
'Boston******'
'******Boston'
Liverpool
Boston
New York
Philadelphia
'-005'
Replace
'PHILADELPHIA'
Spitting and Joining
'Here,Are,Some,Words'
Split on comma
['Here', 'Are', 'Some', 'Words']
Joining
'Here:Are:Some:Words'
Split on newline
'Sometimes we are given\na multiline document\nas a single string'
Sometimes we are given
a multiline document
as a single string
Slicing
'W'
'r'
'Doerf'
'flinger'
More information: common sequence operations
4.4 Learn to use unicode
There are multiple encoding possible for mapping characters to bytes. Python strings default to UTF-8. Earlier versions of Python used a more limited encoding.
Encode
'ππ'
'ππ'
'Π'
928
'Π'
'ꀀabcd\u07b4'
Saving File in Unicode
Søme Unˆcode text