I went to coding boot camp where we learned Ruby as our backend language. While Ruby is nice and Ruby on Rails is amazing, let's face it, it's an endangered language in much the same way Hawaiian is. So I've spent the past two weeks learning Express. I can safely say that I dislike having to type everything out. Where's that automagic that Ruby on Rails provides?
I've finally settled on learning Python because of Flask and Django. From what I understand, Flask is much like Sinatra (I get PTSD even thinking about that) and Django is Python's Rails. Really, all I want is to be able to easily implement CRUD functionality using HTTP requests in a jiffy.
So here are some of the things I've learned with Automate the Boring Stuff with Python concerning string methods:
Uppercase and Lowercase Your Strings
spam = 'Hello Quarantined World!'
spam.upper()
# 'HELLO QUARANTINED WORLD!'
The upper()
method makes all of the characters in your string uppercase. Remember, strings in Python are immutable. This means when you call spam
again you will get back 'Hello Quarantined World!'. If you want to save the new value of spam you would have to enter spam = spam.upper()
.
The lower()
string method returns your string in all lowercase. Both of these methods are normally used when comparing two values:
name = 'McLovin'
name == 'Mclovin'
# false
name.lower() == 'mclovin'
# true
In the above example, you can see how to use one of the functions to help compare two strings easier.
Test to See if Your Strings are Upper or Lower
The islower()
function will return true if the entire string is lowercase. Conversely, isupper()
will return true if the entire string is uppercase.
spam = 'I want to go outside!'
spam.islower()
# false
spam.lower().islower()
# true
In the above examples islower()
returns false
because the letter "I" is in uppercase. For showing purposes, in the line below, spam.lower().islower()
returns true
because first we execute the lower()
function which makes the entire string lowercase. Then we ask if the string is lowercase. This is called method chaining!
Checking For Letters Only
islower()
and isupper()
are one many methods that begins with "is". All of them return a boolean value.
The isalpha()
method checks to see if a string contains only letters (no numbers).
name = 'Kevin'
name.isalpha()
# true
fullName = 'Kevin McCallister'
fullName.isalpha()
# false
The first one is obviously true but the second one returns false
because of the whitespace in between the first and last name.
Checking For Numbers Only
year = 2020
year.isdecimal()
# true
Checking For Letters and Numbers
pandemic = 'Covid-19'
pandemic.isalnum()
# false
pandemic = 'Covid19'
pandemic.isalnum()
# true
Checking For Whitespace
animal = ' Pangolin'
animal.isspace()
# false
animal[0].isspace()
In the first example above, isspace()
returns false because there are letters in the string, however, animal[0].isspace()
returns true
because we are accessing the first index is a space.
Is This A Title?
article = 'How to Wash Your Hands'
article.istitle()
# false
article = `How To Wash Your Hands`
article.istitle()
# true
This method is an interesting one. It will return true only if the first letter of a word is uppercase and the following character is lowercase (if it exists). The first execution of istitle()
returns false
because the word "to" is not "To". Also, you can turn any string into titlecase by using title()
!
Starts or Ends With
toiletPaperCount = 'too much to count'
toiletPaperCount.startswith('t')
# true
toiletPaperCount.startswith('too')
# true
toiletPaperCount.endswith('ount.')
#true
There are a whole lot more string methods to learn. If you want to know more, look into join, split, strip, rstrip, rjust, center, and replace! Learning Python has been really fun and I love seeing the differences in syntax when compared to JavaScript and even Ruby. I'll be continuing this course while blogging about it!
Stay home to stay safe!
Top comments (4)
But in the article,
toiletPaperCount = 'too much to count'
. I guess it's a typo thenGood catch!
And it's
false
, isn't it?toiletPaperCount
ends withount
, notount.
Check out
help(str)
will get you most of the way there.