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:
spam = 'Hello Quarantined World!' spam.upper() # 'HELLO QUARANTINED WORLD!'
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().
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.
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
false because the letter "I" is in uppercase. For showing purposes, in the line below,
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!
isupper() are one many methods that begins with "is". All of them return a boolean value.
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.
year = 2020 year.isdecimal() # true
pandemic = 'Covid-19' pandemic.isalnum() # false pandemic = 'Covid19' pandemic.isalnum() # true
animal = ' Pangolin' animal.isspace() # false animal.isspace()
In the first example above,
isspace() returns false because there are letters in the string, however,
true because we are accessing the first index is a space.
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
false because the word "to" is not "To". Also, you can turn any string into titlecase by using
toiletPaperCount = 'too much to count' toiletPaperCount.startswith('t') # true toiletPaperCount.startswith('too') # true toiletPaperCount.endswith('ount.') #true
Stay home to stay safe!
Start building your DEV profile
Level up every day