0% found this document useful (0 votes)
40 views75 pages

Chapter 8 Lesson 8.10 - 8.12 String Manipulation, Nested Statements and Subroutine

The document provides examples of using string manipulation functions like len(), substring slicing, and upper()/lower() in Python. It also covers nested statements like if/else conditions within for loops. Some challenges provided include writing programs to count vowels/consonants in a string, get initials from a full name, and reverse a string. The document also introduces the concept of arrays and subroutines/functions in Python. It provides examples of defining functions to perform math operations and calling them within a menu-driven calculator program.

Uploaded by

Gicel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views75 pages

Chapter 8 Lesson 8.10 - 8.12 String Manipulation, Nested Statements and Subroutine

The document provides examples of using string manipulation functions like len(), substring slicing, and upper()/lower() in Python. It also covers nested statements like if/else conditions within for loops. Some challenges provided include writing programs to count vowels/consonants in a string, get initials from a full name, and reverse a string. The document also introduces the concept of arrays and subroutines/functions in Python. It provides examples of defining functions to perform math operations and calling them within a menu-driven calculator program.

Uploaded by

Gicel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 75

LESSON 8.

10 STRING MANIPULATIONS
stringlength = len("Hi")
print(stringlength)

stringlength = len("Science")
print(stringlength)

OUTPUT: save@filename: stringlength


2
7
stringlength = len(“ Complete Name ")
print(stringlength)

OUTPUT: save@filename: stringlength2


?
substring = (“Hello")[0:1]
print(substring)
substring = (“Hello")[2:4]
print(substring)
substring = (“Hello")[3:5]
print(substring)

OUTPUT: save@filename: substring


substring = ("10 Social")[4:12]
print(substring)

OUTPUT: save@filename: substring


stringlength =len(“INFORMATION TECHNOLOGY”)
substring1 = ("INFORMATION TECHNOLOGY")[0:4]
substring2 = ("INFORMATION TECHNOLOGY")[12:16]
print(substring1 + “ “ + substring2)
print(stringlength)

Output:
INFO TECH
22 Save @ filename: Substring_Stringlength.py
substring1 = ("10 Social")[0:2]
substring2 = ("10 Social")[3:10]
print(substring1)
print(substring2)

OUTPUT:
10 save @ filename: substring2.py
Social
InputString = input("Enter a string:")
stringlength = len(InputString)
print(InputString, "is", str(stringlength), "characters long")

OUTPUT: save@filename: InputString_Length

Enter a string: SAMPLE


InputString is 6 characters long
Python

Word = “Hello”.upper()

Word = “Hello”.lower()
InputString1 = input("Enter a string lowercase:").upper()
InputString2 = input("Enter a string uppercase:").lower()

print(InputString1)
print(InputString2)
OUTPUT: save@filename: upper_lower

Enter a string in lowercase: sample


Enter a string in uppercase: SAMPLE
SAMPLE
sample
firstname= input("Enter first name: ")
length1= len(firstname)
substring1= (firstname[0:1]).upper()
substring2= firstname[1:length1].lower()
print(substring1 + substring2)

OUTPUT: save@filename: firstletter_upper


Enter first name: cynnie
Cynnie
ASSIGNMENT#3
Please read ahead Lesson 8.11 p.251

Using nested statement create Python program code that will count the
number of vowels in a string and number consonants.
Vowels = a, e, i, o, and u
Consonants = all letter except those letters above
Lesson 8.11: NESTED STATEMENTS
Nested statements - a condition within a condition,
or a loop within a loop, or a condition in a loop, or a loop
within a condition

• IF – THEN in an IF-ELSE
• IF – THEN in a FOR LOOP
• FOR LOOP in an IF – ELSE or IF - THEN
• FOR LOOP within a FOR LOOP
moreThan10 = 0
equalTo10 = 0
for x in range(0, 10):
number = int(input("Enter a number "))
if number >= 10:
moreThan10 = moreThan10 + 1
else:
equalTo10 = equalTo10 + 1
print(equalTo10)
print(moreThan10)

save@filename: nested_loop1
less = 0 save@filename: nested_loop2
greater = 0
num = 1

while num != 0:
num = int(input("Enter number: "))
if num >= 100:
greater = greater + 1
else:
less = less + 1
print(greater)
print(less)
Questions
24. Give one example of a nested statement
25. What will the pseudocode statement return LENGTH(“Hello World!”) return?
26. What will the pseudocode statement return SUBSTRING(“Hello World!”, 6, 5)
return?
27. Write a program to take a string input from the user, and count how many
numbers are in the string and how many are letters and special symbols.
28. Write a program to output the string value backwards
choice = int(input("Enter 1 or 2:"))
if choice == 1:
word = input("Enter a word")
for count in range(0, len(word)):
character = word[count:count+1]
if character == "a" or character =="e" or character == "i" or character == "o" or character == "u":
print(character)

OUTPUT: save@filename: count_vowels


Science
iee
save @ filename: Nested_Loop_sample
QUESTION# 24
moreThan10 = 0
equalTo10 = 0
for x in range(0, 10):
number = int(input("Enter a number "))
if number > 10:
moreThan10 = moreThan10 + 1
else:
number = 10
equalTo10 = equalTo10 + 1
print(equalTo10)
print(moreThan10)
save @ filename: String_length_HW
QUESTION# 25
stringlength = len("Hello World!")
print(stringlength)

OUTPUT:

12
save @ filename: String_length_HW
QUESTION# 26
substring1 = (“HELLO WORLD!")[6:5]
print(substring1)

OUTPUT:
(no output)
QUESTION# 27 save @ filename: NUMBERS_LETTERS_count
numbers = 0
letters = 0
stringInput = input("Enter a message: ")
for count in range(0, len(stringInput)):
character = stringInput[count:count+1]
if character == "0" or character == "1" or character == "2" or character == "3" or
character == "4" or character == "5" or character == "6" or character == "7" or
character == "8" or character == "9":
numbers = numbers + 1
else:
letters = letters + 1
print("The are" + " " + str(numbers) + " " + "numbers" + " " + "in the string you entered")
print("The are" + " " + str(letters) + " " + "letters" + " " + "in the string you entered")
OUTPUT:
INFORMATION TECHNOLOGY 123
The are 3 numbers in the string you entered
The are 23 letters in the string you entered
save @ filename: reverse_input_string
QUESTION# 28
InputString = input("Enter a string: ")
reverse_string = InputString[::-1]
print(reverse_string)

OUTPUT:
SAMPLE
ELPMAS
Challenge Activity:
Create a program in Python that will determine the number of vowels
in a string and number of consonants.

Save @ filename: Vowels_Consonants_number


Assignment # 3:

• Read ahead Lesson 8.12: Subroutine.


• Make sure the answers in Questions 24 – 28
are reflected in your notebook p. 255
Challenge Activity save @ filename: Consonants_Vowels_count
vowels = 0
consonants = 0
stringInput = input("Enter a message:")
for count in range(0, len(stringInput)):
character = stringInput[count:count+1]
if character == "a" or character == "e" or character == "i" or character == "o" or
character == "u" or character == "A" or character == "E" or character == "I" or
character == "O" or character == "U":
vowels = vowels + 1
else:
consonants = consonants + 1
print("vowels:", vowels)
print("consonants:", consonants)
OUTPUT:
Enter a message: Science
vowels: 3
consonants: 4
Challenge Activities

1. Create a program that will display your FULLNAME INITIALS


examples: Kenlley Hervine Siarill OUTPUT: KHS
save @ filename: Challenge2 (5 PTS.)

2. Create program that will count the number of “A” and “a” in a string.
save @ filename: Challenge3 (5PTS.)

3. Create program that will change the format of the string in capital or UPPER
and display in reverse format.
save @ filename: Challenge4 (5 PTS.)
Challenge Activity # 1:
Challenge Activity # 2:
Challenge Activity # 2:
Challenge Activity # 3:
Array – storing data in a single or one memory name (location) with the
same data type.
Element = a slot or memory space in an array
0 1 2 3 4 5 6 7 8 9
12 32 45 8 9 10 3 5 189 98

arrayData[0] = 12
arrayData[1] = 32
arrayData[2] = 45
arrayData[3] = 8
arrayData[4] = 9
arrayData[5] = 10
arrayData[2] = 3
arrayData[3] = 5
arrayData[4] = 189
arrayData[5] = 98
greater = 0
save@filename: compare_value_array.py
less = 0
arrayData = []
for x in range (10):
arrayData.append(int(input("Enter a number: ")))
if arrayData[x] < 10:
less = less + 1
else:
greater = greater + 1
print(arrayData)
print(less)
print(greater)
LESSON 8.12: SUBROUTINES
OUTPUT:
OUTPUT:
def add(n1, n2):
answer = n1 + n2 n1 = int(input("Enter a number: "))
return(answer) n2 = int(input("Enter a number: "))

def subtract(n1, n2): menuOption = input("Enter option: +, -, *, /, %, **, //:")


answer = n1 - n2 if menuOption == "+":
return(answer) result = add(n1, n2)
print(result) elif menuOption == "%":
def multiply(n1, n2):
result = modulus(n1, n2)
answer = n1 * n2
return(answer) elif menuOption == "-": print(result)
result = subtract(n1, n2)
def divide(n1, n2): print(result) elif menuOption == "//":
answer = n1 / n2 result = floor_div(n1, n2)
return(answer) elif menuOption == "*": print(result)
result = multiply(n1, n2)
def exponent(n1, n2): print(result) else:
answer = n1 ** n2
return(answer)
print("Enter a valid operator")
elif menuOption == "/":
def modulus(n1, n2): result = divide(n1, n2)
answer = n1 % n2 print(result)
return(answer)
elif menuOption == "**":
def floor_div(n1, n2): result = exponent(n1, n2)
answer = n1 // n2 print(result)
return(answer)
OUTPUT
Insert code to populate the array

OUTPUT
OUTPUT:
OUTPUT:
OUTPUT:
Answer # 29:

The difference between a function and procedure:

Procedure performs the code inside the main program and does not
return a value.

Function returns a value to the program that calls it, this can be done
either using the command RETURN, or saving the value to the function’s
identifier.
Answer # 30:
def calculate(num1, num2):
num1 = 0
num2 = 0
num1 = int(input("Enter a number"))
num2 = int(input("Enter another number"))
num1 = num1 * 2
num2 = num2 + num1
return num1 + num2

print(str(calculate(1, 2)))
Answer #
31:

ini_list = [[1, 2, 5, 10, 7],


[4, 3, 4, 3, 21],
[45, 65, 8, 12, 9, 9]]

elem_to_find = int(input("Enter a number: "))

res1 = any(elem_to_find in sublist for sublist in ini_list)


if res1 == True:
total1 = elem_to_find + elem_to_find
print("The number you entered is in the list (number + input)" + " " + str(total1))
else:
print("The number you entered is not in the list")
Answer # 31:
Answer # 32
def highest():
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
if num1 > num2 and num1 > num3:
print("The first number" + " " + str(num1) + " " + "you entered is the highest")
elif num2 > num1 and num2 > num3:
print("The second number" + " " + str(num2) + " " + "you entered is the highest")
else:
print("The third number" + " " + str(num3) + " " + "you entered is the highest")
highest()
Answer # 32
Answer # 33
Answer # 33
string1 = input("Enter a string: ")
string2 = input("Enter a string: ")
substring1 = string1[0:3]
substring2 = string2[0:3]
print(substring1 + " " + substring2)
Answer # 33

You might also like