1.
String slicing
fruit = “Strawberry”
drink = “Smoothie”
Code Output
print(fruit[3])
print(drink[6])
print(fruit[2:6])
print(drink[1:5])
print(fruit[-2])
print(drink[-4])
print(fruit[5:])
print(drink[3:])
print(fruit[:4])
print(drink[:3])
2. String Methods
a) Using Python, write a short program that:
- assigns the value “strawberry” to a string called fruit
- converts it to an upper-case string and resaves it as upperFruit
- prints the variable upperFruit
b) Using Python, write a short program that:
- assigns the value “SMOOTHIE” to a string called drink
- converts it to an lower-case string and resaves it as lowerDrink
- prints the variable lowerDrink
3. Using Python, write a short program that:
- assigns a value “strawberry” to a string called fruit
- counts the number of times the letter r appears and saves it as
rCount
- print the variable rCount
4. Write the output for the following program.
fruit =”Pineapple” Output:
print([Link](“p”))
fruit =”banana” Output:
print(len(fruit))
fruit1 =”banana” Output:
fruit2 = “Pineapple”
if len(fruit1)>len(fruit2):
print(fruit1, “has longer name.”)
else:
print(fruit2, “The second fruit has
longer name.”)
a = 14
b = 4 Output:
print(a*b)
print(a/b)
print(a%b)
print(a//b)