Python Tutorial 4
Python Tutorial 4
S= ‘01234566789’
Now write expressions using string s and the indexing operator that evaluate to:
(a) ‘234’
(b) ‘78’
(c) ‘1234567’
(d) ‘0123’
(e) ‘789’
Syntax:
s = "0123456789"
a = s[2:5]
print("(a)"+ a)
b = s[7:9]
print("(b)"+ b)
c = s[1:8]
print("(c)"+ c)
d = s[0:4]
print("(d)"+ d)
e = s[7:10]
print("(e)"+ e)
Output:
Assuming that variable forecast has been assigned string
‘It will be a sunny day today’
(a) To variable count, the number of occurrences of string ‘day’ in string forecast.
(b) To variable weather, the index where substring ‘sunny’ starts.
(c) To variable change, a copy of forecast in which every occurrence of substring
‘sunny’ is replaced by ‘cloudy’.
Syntax:
forecast=("It will be a sunny day today")
count=forecast.count("sunny")
print(count)
index=forecast.index("sunny")
print(index)
replace=forecast.replace("sunny","cloudy")
print(replace)
Output:
Write a statement that prints the values of variables last, first, and middle in one
line, separated by a horizontal tab character. (The Python escape sequence for the
horizontal tab character is \t.) If the variables are assigned like this:
>>> last = ‘Smith’
>>> first = ‘John’
>>> middle = ‘Paul’
Syntax:
last = "Smith"
first = "John"
middle = "Paul"
print(last,first,"\t",middle)
Output:
Write function even() that takes a positive integer n as input and prints on the
screen all numbers between, and including, 2 and n divisible by 2 or by 3, using
this output format:
Expected Output:
Syntax:
def even():
for x in range(2,int(n)):
if x % 2 == 0 or x%3 ==0:
print (x, end=" ")
n=input("Enter positive integer n:")
even()
Output:
Assume variables first, last, street, number, city, state, zipcode have already been
assigned. Write a print statement that creates a mailing label:
Expected Output:
John Doe
First = ‘John’
Last = ‘Doe’
Number = 123
City = ‘AnyCity’
State = ‘AS’
Zipcode = ‘09876’
Syntax:
first = "John"
last = "Doe"
street = "main Street"
number = 123
city="AnyCity"
state="AS"
zipcode="09786"
print(first,last)
print(number,street,city + ",",state,zipcode)
Output:
Implement function roster() that takes a list containing student information and
print out a roster, as shown below. The student in formation, consisting of the
student’s last name, first name, class, and average course grade, will be stored in
that order in a list. Therefore, the input list is a list of lists. Make sure the roster
printed out has 10 slots for every string value and 8 for the grade, including 2 slots
for the decimal part.
Expected Output:
students = []
students.append(["Last","First" ,"class","Average Grade"])
students.append(["DeMoines", "Jim", "Sophomore", 3.45])
students.append(["Pierre", "Sophie", "Sophomore", 4.0])
students.append(["Columbus", "Maria", "Senior", 2.5])
students.append(["Phoenix", "River", "Junior", 2.45])
students.append(["Olympis", "Edgar", "Junior", 3.99])
roster(students)
Syntax:
def roster():
students = []
students.append(["Last","First" ,"class","Average Grade"])
students.append(["DeMoines", "Jim", "Sophomore", 3.45])
students.append(["Pierre", "Sophie", "Sophomore", 4.0])
students.append(["Columbus", "Maria", "Senior", 2.5])
students.append(["Phoenix", "River", "Junior", 2.45])
students.append(["Olympis", "Edgar", "Junior", 3.99])
Syntax:
def stringCount():
filename=open(input("Enter file name:"),"r")
x=filename.read().split()
line=print(x.count(input("target string:")))
return line
stringCount()
Output: