We will cover some of the builtin string methods in Python
part of #100DaysofCode Python Edition follow along at https://jcutrer.com/100daysofcode
Python Docs Reference: https://docs.python.org/3/whatsnew/2.0.html#string-methods
Some sample data for this excerise.
a = "python"
b = "Today is a good day to start learning python."
mit = """THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE."""
len(a) # returns strings length in number of characters
a.upper()
a.lower()
b.capitalize()
a.islower() # testing for all lower case characters
a.isupper() # testing for all upper case characters
str.title() will capitalize each word in a string
mit.title()
a.center(80, "=") #pad and center a string
str.count()
returns the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
mit.count('THE') # count the occurences of a substring in a string
str.strip()
is equivalent to trim()
in some other languages
" Well Hello There ".strip()
str.casefold()
is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string.
mit.casefold()
weekdays_csv = "monday,tuesday,wednesday,thursday',friday,saturday,sunday"
weekdays_csv.split(",") # returns a list
mit.splitlines()
a[0] + a[-1] # first and last characters
a[:3] # first 3 characters
b[:-10] # first 10 characters
b[-10:] # last 10 characters
a * 5 # multiply a string
a * 5 + b # multiply then add another string
a / 5 # cannot divide a strings
a - a
b - a # cannot substract strings
This notebook is part of my #100DaysofCode Python Edition project.