Slide 3
Slide 3
MICHEAL OGUNDERO
EMAIL – OGUNDEROAYODEJI@GMAIL.COM
QUESTIONS FROM LAST CLASS
Escape Codes
• Now try to output sillyTest again
sillyTest
• The answer looks strange! It indicates an alternate way to encode the string internally in Python using
escape codes.
• Escape codes are embedded inside string literals and start with a backslash character \.
• They are used to embed characters that are either unprintable or have a special syntactic meaning to
Python that you want to suppress.
Below is some of the escommon escape codes
Escape Meaning
code
\\ Backslash
\n Newline
\’ Single quote character
\” Double quote character
print('a\nb\n\nc')
EXERCISE
Create a Python script that uses escape characters to format and print the following text:
A method is a function that “belongs to” an object. String Methods are functions that belong to the string
object.
Methods are specific to the data type for a particular variable. So there are some built-in methods that
are available for all strings, different methods that are available for all integers, etc.
They are called using dot notation.
For example, lower() is a string method that can be used like this, on a string called "sample string":
sample_string.lower().
Below is an image that shows some methods that are possible with any string.
Each of these methods accepts the string itself as the first argument of the method. However, they also
could receive additional arguments, that are passed inside the parentheses.
This method is very useful with print statements. It is used to format the output of a string.
For example, lower() is a string method that can be used like this, on a string called "sample string":
sample_string.lower().
animal = "dog"
action = "bite"
print("Does your {} {}?".format(animal, action))
# Does your dog bite?
Notice how in each example, the number of pairs of curly braces {} you use inside the string is the same
as the number of replacements you want to make using the values inside format()
STRING METHODS: split()
This method returns a data container called a list that contains the words from the input string.
The split method has two additional arguments (sep and maxsplit).
The sep argument stands for "separator". It can be used to identify how the string should be split up (e.g.,
whitespace characters like space, tab, return, newline; specific punctuation (e.g., comma, dashes)). If the sep
argument is not provided, the default separator is whitespace.
True to its name, the maxsplit argument provides the maximum number of splits. The argument gives
maxsplit + 1 number of elements in the new list, with the remaining string being returned as the last
element in the list.
new_str.split(' ', 3)
# ['The', 'cow', 'jumped', 'over the moon.']
new_str.split('.')
# ['The cow jumped over the moon', '']
EXERCISE
Below, we have a string variable that contains the first verse of a poem. Remember, \n is a special
sequence of characters that causes a line break (a new line).
verse = "If you can keep your head when all about you\n Are losing theirs and blaming it on you,\nIf you
can trust yourself when all men doubt you,\n But make allowance for their doubting too;\nIf you can wait
and not be tired by waiting,\n Or being lied about, don’t deal in lies,\nOr being hated, don’t give way to
hating,\n And yet don’t look too good, nor talk too wise:"
Answer the following questions about verse using python code and properly formatting your output:
• The statements are executed in the order they appear in the text of the program: sequentially. This is
the simplest way for the execution of the program to flow.You will see instructions later that alter that
natural flow.
INPUT AND OUTPUT
Needing to convert string input to numbers is a common situation, both with keyboard input and later in
web pages.
xString = input("Enter a number: ")
x = int(xString)
yString = input("Enter a second number: ")
y = int(yString)
print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='')
INPUT AND OUTPUT
Write a program that asks for three numbers, and lists all three, and their sum.
INPUT AND OUTPUT
• the object is the string ’Hello { }!’. The method is named format. There is one further parameter,
person.
• Optional elaboration with explicitly numbered entries Imagine the format parameters numbered in
order, starting from 0. In this case 0, 1, and 2. The number of the parameter position may be included
inside the braces