String Processing and Formatting

CS 65: Introduction to Computer Science I

Review: Useful string methods we've used

  • rstrip() - strip out newlines/whitespace at the end of a string
  • split() - split a string into a list of strings based on a delimeter
  • index() - find a substring inside a string
  • count() - count the number of times a substring appears in a string

Other string methods to know about

It's always good to be aware of a whole bunch of string methods - they can come in very handy.

See https://www.w3schools.com/python/python_ref_string.asp

Here's a sample:

In [1]:
name = "Eric"
#check if all the characters are lowercase
name.islower()
Out[1]:
False
In [2]:
lowercase_name = name.lower()
lowercase_name
Out[2]:
'eric'
In [3]:
name1 = "eric"
name2 = "Titus"
name1.lower() < name2.lower()
Out[3]:
True
In [4]:
name1 = "eric"
name1.capitalize()
Out[4]:
'Eric'

Here's a potential problem with user input:

In [5]:
user_input = float(input("Enter a number: "))
print("Your number divided by 2 is",user_input/2)
Enter a number: ten
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-63a2d6e7a2b7> in <module>
----> 1 user_input = float(input("Enter a number: "))
      2 print("Your number divided by 2 is",user_input/2)

ValueError: could not convert string to float: 'ten'

Here's a fix

In [6]:
user_input = input("Enter a number: ")
if user_input.isnumeric():
    user_input = float(user_input)
    print("Your number divided by 2 is",user_input/2)
else:
    print("That's not a number!")
Enter a number: ten
That's not a number!

Some formatting annoyances

Perhaps you have tried to do something like this, and you end up with a space between the $ and the number that you don't want.

In [7]:
employee_num = 123
pay = 980.543
print("Employee",employee_num,"made $",pay,"this period.")
Employee 123 made $ 980.543 this period.

The format() method

The format() method allows you to substitute values into placeholders within a string.

In [8]:
employee_num = 123
pay = 980.543
pay_message = "Employee {n} made ${p} this pay period."
print( pay_message.format(n=employee_num, p=pay) )
Employee 123 made $980.543 this pay period.

You can even do it in one step:

In [9]:
employee_num = 123
pay = 980.543
print( "Employee {n} made ${p} this pay period.".format(n=employee_num, p=pay) )
Employee 123 made $980.543 this pay period.

You can even add formatting information to the placeholders for things like how to format numbers.

In this example ":.2f" means format as a floating-point number with 2 places to the right of the decimal point.

In [10]:
employee_num = 123
pay = 980.543
print( "Employee {n} made ${p:.2f} this pay period.".format(n=employee_num, p=pay) )
Employee 123 made $980.54 this pay period.

":.0%" means format as a percentage with 0 decimal places

In [11]:
print("{name} earned a {perc_grade:.0%} on the test".format(name="Eric",perc_grade=.897))
Eric earned a 90% on the test

How can you know how to use all of these things?

You just have to know it's a thing, and look it up when you need to use it.

https://docs.python.org/3/library/string.html#formatstrings

https://www.w3schools.com/python/ref_string_format.asp

Review: Character representation

Every text character is represented by a numerical code in your computer's memory.

They came up with in the 1960's as part of the ASCII standard (https://en.wikipedia.org/wiki/ASCII)

Here's some examples

code character
33 !
34 "
35 #
36 $
37 %
code character
65 A
66 B
67 C
68 D
69 E
code character
97 a
98 b
99 c
100 d
101 e

You can use the built-in ord() function to look up the code for any given character. Use chr() to do the opposite.

In [12]:
ord("A")
Out[12]:
65
In [13]:
chr(65)
Out[13]:
'A'

In the lab, we are going to use the functions to do some encryption on strings.