Lecture 3@StringDataStructure
Lecture 3@StringDataStructure
Ghulam Abbas
What is String in Python?
Output
How to access characters in a string?
We can access individual characters using indexing and a
range of characters using slicing.Index starts from 0.
Trying to access a character out of index range will raise
an IndexError.
The index must be an integer.
We can't use float or other types, this will result into TypeError.
Output
How to access characters in a string?
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last
item and so on.
We can access a range of items in a string by using the
slicing operator (colon).
Output
Syntax Errors
If we try to access index out of the range or use decimal
number, we will get errors.
Slicing
To extract substring from the whole string then we use the
syntax like
beginning represents the starting index of string
end denotes the end index of string which is not inclusive
steps denotes the distance between the two words.
Note: We can also slice the string using beginning and only
and steps are optional.
Slicing can be best visualized by considering the index to be
between the elements as shown below.
If we want to access a range, we need the index that will
slice the portion from the string.
Example
How to change or delete a string?
Strings are immutable. This means
We cannot delete or remove
that elements of a string cannot be
characters from a string.
changed once it has been
But deleting the string entirely is
assigned. We can simply reassign
possible using the keyword del.
different strings to the same name.
Updating Strings
"update" an existing string by (re)assigning a variable to
another string.
The new value can be related to its previous value or to a
completely different string altogether. For example
Python String Methods
Output
Set-2 len & count
len() : This function returns the length of the string. :- This function returns
the length of the string.
count(“string”, beg, end): This function counts the occurrence of
mentioned substring in whole string. This function takes 3 arguments,
substring, beginning position( by default 0) and end position(by default
string length).
Center, ljust & rjust
center():This function is used to surround the string with a character repeated
both sides of string multiple times. By default the character is a space. Takes 2
arguments, length of string and the character.
ljust(): This function returns the original string shifted to left that has
a character at its right. It left adjusts the string. By default the character is space.
It also takes two arguments, length of string and the character.
rjust(): This function returns the original string shifted to right that has
a character at its left. It right adjusts the string. By default the character is space.
It also takes two arguments, length of string and the character.
example
Isalpha, isalnum & isspace
isalpha() :- This function returns true
when all the characters in the string
are alphabets else returns false.
isalnum() :- This function returns true
when all the characters in the string
are combination of numbers and/or
alphabets else returns false.
isspace() :- This function returns true
when all the characters in the string
are spaces else returns false
join
join(): This function is used
to join a sequence of strings
mentioned in its arguments
with the string.
Set 3: strip, lstrip, rstrip
strip(): This method is used to delete
all the leading and
trailing characters mentioned in its
argument.
lstrip(): This method is used to delete
all the leading characters mentioned
in its argument.
rstrip(): This method is used to delete
all the trailing characters mentioned
in its argument.
Min &max