Learn Python Series (#3) - Handling Strings Part 2

Learn Python Series (#3) - Handling Strings Part 2

python_logo.png

What Will I Learn?

  • You will learn about some more sequence type operations,
  • and about how-to use built-in string manipulation methods,
  • and about built-in string Truth Value Testing methods.

Requirements

  • A working modern computer running macOS, Windows or Ubuntu
  • An installed Python 3(.6) distribution, such as (for example) the Anaconda Distribution
  • The ambition to learn Python programming

Difficulty

Basic / Intermediate

Curriculum (of theLearn Python Series):

Learn Python Series (#3) - Handling Strings Part 2

In the previous tutorial episode about how to handle strings in Python 3, we covered the creation of strings, how to concatenate and repeat them into forming longer strings, how to index and slice substrings, and how to format the printing of strings via C-like printing and the newer format() method.

In this tutorial episode we'll expand our knowledge by looking into (some of) the built-in sequence type and/or string manipulation functions / methods.

Some more common sequence operations

In the Intro episode we covered a lot, but regarding common sequence operations, the following ones were not explicitly covered yet:

The count() method

The count(x) method returns the number of occurrences of the x argument in the sequence providing the count() method. For example:

# Count how often the character 'l'
# is found in the word 'hello'
word = 'hello'
num_l = word.count('l')
print(num_l)
# 2

# This also works on longer substrings
# In this case, we're counting the occurrences
# of the substring 'll' in the 'hello'
num_ll = word.count('ll')
print(num_ll)
# 1
2
1

The in operation

We haven't yet explicitly covered the in operation, although it was used when introducing for loops already.
For example:

some_numbers = [1, 3, 5, 7, 9]
for i in some_numbers:
    print(i)

# 1
# 3
# 5
# 7
# 9
1
3
5
7
9

The statement for i in some_numbers:, or in pseudo-code for each_element in sequence:, means that Python traverses over every element contained in that sequence and assigns the values of those elements to a new variable name (i, in this case), which variable name can be used throughout the remainder of the loop body.

But the in keyword can also be used without a for loop. It then tests if the sequence contains a value, and it returns True or False. For example:

some_numbers = [1, 3, 5, 7, 9]
test1 = 1 in some_numbers
test2 = 2 in some_numbers

print(test1, test2)
# True False

if 3 in some_numbers:
    print('Hi'*3)
# HiHiHi
True False
HiHiHi

The not in operation

Returns the opposite of in: True if the sequence does not contain a value, and False if it does.

some_numbers = [1, 3, 5, 7, 9]
test1 = 1 not in some_numbers
test2 = 2 not in some_numbers

print(test1, test2)
# False True
False True

Built-in string methods

str.find()

Usage: str.find(sub[, start[, end]])

find() searches for a substring contained in a larger string. It functions the same, more or less, as the in operator, but it returns the lowest index number (position) of where the substring was first found (from left to right) in the larger string. If find() doesn't find the substring in the string, it returns -1.

str1 = "I love Python!"
index = str1.find('o')
print(index)
# 3
# Notice there are two 'o's in the string,
# index 3 is the lowest index an 'o' was found.

index = str1.find('Py')
print(index)
# 7
3
7

str.rfind()

Usage: str.rfind(sub[, start[, end]])

Just as str.find(), but rfind() returns the highest index at which the substring was found.

str1 = "I love Python!"
index = str1.rfind('o')
print(index)
# 11
# Notice there are two 'o's in the string,
# index 11 is the highest index an 'o' was found.
11

str.replace()

Usage: str.replace(old,new[,count])

The replace() method searches for the occurrence of the substring old inside the total string, and when it finds that substring it will be replaced by the substring new, in effect returning a copy of the original string.

Optionally we can add a count (integer) argument, to only replace the first count occurences of the substring found.

str_original = 'Mary had a little lamb'

# Let's change `lamb` with `dog`
str_new1 = str_original.replace('lamb', 'dog')
print(str_new1)
# Mary had a little dog

# Let's search for a substring that isn't in the string
str_new2 = str_original.replace('cat', 'fish')
print(str_new2)
# PS: nothing was replaced
# Mary had a little lamb

# Let's replace only the first `a` with an `o`
str_new3 = str_original.replace('a', 'o', 1)
print(str_new3)
# PS: Only Mary's name will be changed to Mory now...
# Mory had a little lamb
Mary had a little dog
Mary had a little lamb
Mory had a little lamb

str.join()

Usage: str.join(iterable)

The join() method iterates over the function argument iterable, which can be any iterable as long as it contains strings, and concatenates every iteration found (in the case of strings: characters, and in the case of lists: list elements) with the string providing the method as the returned-string's character separator.

# Let's add a space separator
str_original = 'Let the sun shine'
str_new = ' '.join(str_original)
print(str_new)
# L e t   t h e   s u n   s h i n e

# Let's add `*-*` as the separator
str_original = 'Stars and Stripes'
str_new = '*-*'.join(str_original)
print(str_new)
# PS: please notice no separators are added after the final
# iterated character.
# S*-*t*-*a*-*r*-*s*-* *-*a*-*n*-*d*-* *-*S*-*t*-*r*-*i*-*p*-*e*-*s

# If we pass in a list of strings, not every character
# but every list element is split by the separator string
list_original = ['Python', 'Is', 'Fun']
str_new = ', '.join(list_original)
print(type(str_new), str_new)
# And indeed, a string is returned, not a list:
# <class 'str'> Python, Is, Fun
L e t   t h e   s u n   s h i n e
S*-*t*-*a*-*r*-*s*-* *-*a*-*n*-*d*-* *-*S*-*t*-*r*-*i*-*p*-*e*-*s
<class 'str'> Python, Is, Fun

str.split()

Usage: str.split(sep=None, maxsplit=-1)

The split() method is, more or less, the opposite of join(). It returns a list of elements contained in the string that's providing the split() method.

str_original = 'Steem on, Dream on'

# Split string into words
# by not passing in in a separator (`sep`) argument,
# therefore spaces are used as the separator string
list_new1 = str_original.split()
print(list_new)
# Notice that the comma behind the word `on` is 
# treated as part of the word `on`
# ['Steem', 'on,', 'Dream', 'on']

# Now let's split by the string `', '`
list_new2 = str_original.split(sep=', ')
print(list_new2)
# ['Steem on', 'Dream on']
['Steem', 'on,', 'Dream', 'on']
['Steem on', 'Dream on']

str.strip()

Usage: str.strip([chars])

The strip() method can be used to "strip away" unwanted characters from a string. The chars argument is optional and can be omitted. If omitted, all spaces from the beginning and from the end of a string will be removed, but not the spaces in between words inside the string.

The strip() method works a little "strange" (maybe?) when you try to use it for the first time by including the chars argument. It works as follows:

  • strip() "scans" the string for every character contained within chars string, and strips those characters from the string,
  • in case the same character is found, every instance of that character will be stripped,
  • whenever a character in the string is found that is not contained in the chars string, then the stripping stops,
  • this is however done to the string starting at the beginning, and starting at the end!

Hopefully the following examples make things a bit clearer:

# Using `strip()` without a `chars` argument, 
# to remove spaces from the beginning and the end.
orig = '    This  is  an example     string.    '
stripped = orig.strip()
print(stripped)
# This  is  an example     string.

# This would work to strip to a domain
url = 'www.steemit.com'
domain = url.strip('w.')
print(domain)
# steemit.com

# And this as well
# PS: I now added a trailing `/` slash
url = 'http://www.steemit.com/'
domain = url.strip('htp:/w.')
print(domain)
# steemit.com

# But this would not
url = 'https://www.steemit.com'
domain = url.strip('htps:/w.')
print(domain)
# eemit.com
This  is  an example     string.
steemit.com
steemit.com
eemit.com

str.lower()

Converts and returns a string where all cased characters become lower case.

message = 'Welcome to Utopian.io!'
lower_case = message.lower()
print(lower_case)
# welcome to utopian.io!
welcome to utopian.io!

str.upper()

Converts and returns a string where all cased characters become upper case.

message = 'Welcome to Utopian.io!'
upper_case = message.upper()
print(upper_case)
# WELCOME TO UTOPIAN.IO!
WELCOME TO UTOPIAN.IO!

Truth Value Testing String methods

str.isdigit()

Returns True if the string contains only digits, False otherwise

str1 = "ABC is easy as 123"
test1 = str1.isdigit()
print(test1)
# False

str2 = "1234567890"
test2 = str2.isdigit()
print(test2)
# True
False
True

str.isalpha()

Returns True if the string contains only alphabetic characters, False otherwise.

str3 = 'Three is the magic number'
test3 = str3.isalpha()
print(test3)
# False <= there are also spaces in the string!

str4 = 'Three'
test4 = str4.isalpha()
print(test4)
# True
False
False
True

str.islower()

Returns True if all cased characters ("letters") inside the string are of lower case, False otherwise.

str5 = '123 this is a test'
test5 = str5.islower()
print(test5)
# True

str6 = 'Testing Testing 123'
test6 = str6.islower()
print(test6)
# False
True
False

str.isupper()

Returns True if all cased characters ("letters") inside the string are of upper case, False otherwise.

str7 = 'JAMES BOND 007'
test7 = str7.isupper()
print(test7)
# True

str8 = 'Testing Testing 123'
test8 = str8.isupper()
print(test8)
# False
True
False

What did we learn, hopefully?

Combined with Handling Strings Part 1, where the indexing, slicing, (repeated) concatenation and formatting of strings was covered, and in the Intro Episode where we covered the most essential Python mechanisms, we've now as well covered some more (general) sequence operations in, not in and the occurence count() method, which can of course be applied to strings as well, plus we've now discussed some built-in string manipulation and truth value testing methods as well.

That's quite a lot we've learned already! In the next episode, which is a Round-Up episode, I'll show you what you can create already only using what's been covered already! Come and find out! See you there!

Thank you for your time!



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
Upload from PC
Video gallery
3 columns
2 columns
1 column
25 Comments