0% found this document useful (0 votes)
5 views2 pages

Python String Slicing and Methods Guide

The document provides examples of string slicing and methods in Python, demonstrating how to manipulate strings such as extracting characters and converting cases. It includes code snippets for counting characters and comparing string lengths. Additionally, it shows output for specific operations involving strings and arithmetic calculations.

Uploaded by

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

Python String Slicing and Methods Guide

The document provides examples of string slicing and methods in Python, demonstrating how to manipulate strings such as extracting characters and converting cases. It includes code snippets for counting characters and comparing string lengths. Additionally, it shows output for specific operations involving strings and arithmetic calculations.

Uploaded by

poezin.computing
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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)

You might also like