Python Language Programming Lab
Laboratory Notes (3)
Topic:
1. Write programs to demonstrate use of strings and its functions
Objective:
Write programs to demonstrate use of strings and its functions
Description:
A string is a sequence of characters. You can access the characters one at a time with
the bracket operator:
fruit = 'banana'
letter = fruit[1]
The second statement extracts the character at index position 1 from the fruit variable and
assigns it to the letter variable. The expression in brackets is called an index.
String functions:
len is a built-in function that returns the number of characters in a string:
>>> fruit = 'banana'
>>> len(fruit)
Output: 6
String Slicing:
A segment of a string is called a slice. Selecting a slice is similar to selecting a
character:
>>> s = 'Monty Python'
>>> print s[6:12]
Output: Python
The operator [n: m] returns the part of the string from the “n-th” character to the “m-th”
character, including the first but excluding the last. Strings are immutable, which means
Laboratory Notes (3)
Topic:
1. Write programs to demonstrate use of strings and its functions
Objective:
Write programs to demonstrate use of strings and its functions
Description:
A string is a sequence of characters. You can access the characters one at a time with
the bracket operator:
fruit = 'banana'
letter = fruit[1]
The second statement extracts the character at index position 1 from the fruit variable and
assigns it to the letter variable. The expression in brackets is called an index.
String functions:
len is a built-in function that returns the number of characters in a string:
>>> fruit = 'banana'
>>> len(fruit)
Output: 6
String Slicing:
A segment of a string is called a slice. Selecting a slice is similar to selecting a
character:
>>> s = 'Monty Python'
>>> print s[6:12]
Output: Python
The operator [n: m] returns the part of the string from the “n-th” character to the “m-th”
character, including the first but excluding the last. Strings are immutable, which means