Strings
1
Strings
Indexing, Iterating, Methods
2
Python collection objects
● Collection objects are objects that can hold any number of arbitrary
elements
● Python provides a way to access elements in a collection object and iterate
over them
● Collection objects:
○ String
○ List
○ Tuple
○ Dictionary
○ Set
3
We cover the following collection objects in the upcoming sessions
4
String Indexing
5
Creating string variables
String variables are variables that hold zero or more characters such as letters, numbers,
spaces, commas and many more
Use type(variable_name) to check the data type of a variable
6
Indexing and slicing
● Indexing is a process of referring an element of an iterable (string, list, tuple, etc)
by its position, called index
● Slicing is a process of getting a subset of elements from an iterable based on
index
● To retrieve an element of the list, we use the index operator []. Iterables are zero
indexed, so [0] returns the 1st item and [1] returns the 2nd item and so on
● Python also allows you to index from the end using a negative number
7
Indexing and slicing
Index position from left 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
L A U R E S S O L U T I O N S
Index position from right -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
8
Using index position to slice a string
string_variable[positive index position]
9
Using index position to slice a string
string_variable[negative index position]
10
Using index position to slice a string
string_variable[start index : stop index]
Note: The stop index does not include that
element. So if the stop index is 11, it would
include elements till 11-1 i.e. 10
11
Using index position to slice a string
string_variable[start index : stop index]
Note: The stop index does not include that
element. So if the stop index is -8, it would
include elements till -8-1 i.e. -9
12
Using index position to slice a string
string_variable[ : stop index]
Note: When the start index is left blank, it
considers the start index as index position
0
13
Using index position to slice a string
string_variable[start index : ]
Note: When the stop index is left blank, it
considers the stop index as the last index
position
14
Using index position to slice a string
string_variable[start index : stop index : step]
Note: By default step is 1. With step as 2,
the number of steps to jump while moving
from start to stop is 2. 15
TRY IT
Using loops, reverse the string “leppek”
16
Built-in Functions
17
A few built-in functions for strings
PRINT
DATATYPE
LENGTH
18
String Methods
19
Built-in Functions & Methods
A function is a set of instructions. Python has many built-in functions like print(),
type(), int(), str() and many more.
A method is very much like a function. The difference is that a method is associated
with an object, like string, list, tuple, dictionary and other type of objects.
20
String method: islower()
Returns True if all characters
in the string are lower case
21
String method: isupper()
Returns True if all characters
in the string are upper case
22
String method: istitle()
Returns True if the words are
in title case
23
String method: isalnum()
Returns True if all characters in the
string are alphanumeric, meaning
alphabet letter (a-z) and numbers (0-9)
24
String method: isalpha()
Returns True if all characters in the
string are in the alphabet
25
String method: isdigit()
Returns True if all characters in the
string are digits
26
String method: split()
Note: The words in the output are placed
within square brackets. This is called a list.
We will discuss shortly about lists.
27
String method: upper()
Converts a string into upper case
Note: The upper() method converts the
characters to uppercase but does not
overwrite the original variable. In order to
make the change permanent, assign it to
the same or a new variable
28
String method: lower()
Converts a string into lower case
Note: The lower() method converts the
characters to lowercase but does not
overwrite the original variable. In order to
make the change permanent, assign it to
the same or a new variable
29
String method: title()
Converts a string into title case
Note: The title() method performs title-
casing but does not overwrite the original
variable. In order to make the change
permanent, assign it to the same or a new
variable
30
String method: capitalize()
Converts only the first character to uppercase
31
String method: swapcase()
Lowercase becomes uppercase and vice versa
32
String method: replace()
Replaces a specified value
33
String method: format()
Formats the specified value(s) and insert them inside the string's placeholders. A
placeholder is defined using curly brackets: {}
Values
Notice how “Klaus” and “36” get placed in
the respective placeholders in the string.
This is achieved using the {} placeholder
and the format() method.
Placeholders
34
String method: format()
Using named values to fill in the placeholder
Named values
Notice how the values of name and age
get placed in the respective placeholders
in the string. This is achieved using the {}
placeholder, named values and the
format() method.
Placeholders
35
String method: reverse()
Returns the index of first occurrence of the substring, if found. Else returns -1.
With no start & end
arguments
With start argument
36
TRY IT
You have a string variable as follows:
quote = “In this World nothing Can be said to be Certain, Except Death and Taxes.”
Write a program to print all the letters which are in uppercase. Use [for] loop.
37
TRY IT
You have a string variable as follows:
quote = “In this World nothing Can be said to be Certain, Except Death and Taxes.”
Write a program to print all the letters which are in uppercase. Use [while] loop.
38
Thank You
39