Class-12 Ch-2 Python Revision Tour-II
Class-12 Ch-2 Python Revision Tour-II
Class-12 Ch-2 Python Revision Tour-II
SEC-10, GURUGRAM
SESSION- 2020-21 CLASS- XII
CH-2 PYTHON REVISION TOUR- II SUBJECT- COMPUTER
TOPIC- STRINGS IN PYTHON SCIENCE
Topics to be Covered:-
Strings
Lists
Tuples
Dictionaries
String:-
String literals in python are surrounded by either single quotation marks, or double quotation marks.
In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a
character data type, a single character is simply a string with a length of 1. Square brackets can be used to
access elements of the string.
Computers do not deal with characters, they deal with numbers (binary). Even though you may see characters
on your screen, internally it is stored and manipulated as a combination of 0's and 1's.
This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and
Unicode are some of the popular encoding used.
In Python, a string is a sequence of Unicode characters. Unicode is an information technology standard for the
consistent encoding, representation, and handling of text expressed in most of the world's writing systems.
The Unicode Standard provides a unique number for every character, no matter what platform, device,
application or language.
• U+2022 •
Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can
be used in Python but generally used to represent multiline strings and docstrings.
Example
# all of the following are equivalent
my_string = 'Hello'
print(my_string)
my_string = "Hello"
print(my_string)
my_string = '''Hello'''
print(my_string)
Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
a = "Hello"
print(a)
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.
#first character
print('str[0] = ', str[0])
#last character
print('str[-1] = ', str[-1])
If we want to access a range, we need the index that will slice the portion from the string.
How to change or delete a string?
Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We
can simply reassign different strings to the same name.
We cannot delete or remove characters from a string. But deleting the string entirely is possible using the
keyword del.
# using +
print('str1 + str2 = ', str1 + str2)
# using *
print('str1 * 3 =', str1 * 3)
String Slicing
To access a range of characters in the String, method of slicing is used. Slicing in a String is done by using a
Slicing operator (colon).
Specify the start index and the end index, separated by a colon, to return a part of the string.
String Length
We can test if a sub string exists within a string or not, using the keyword in.
String Methods
Python has a set of built-in methods that you can use on strings.
Example
Example
The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
String Format
As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:
Example
age = 36
txt = "My name is John, I am " + age
print(txt)
The format() method takes the passed arguments, formats them, and places them in the string where the
placeholders {} are:
Example
Python has a set of built-in methods that you can use on strings.
Note: All string methods returns new values. They do not change the original string.
Method Description
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of where it was found
index() Searches the string for a specified value and returns the position of where it was found
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was
found
rindex() Searches the string for a specified value and returns the last position of where it was
found
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
startswith() Returns true if the string starts with the specified value
swapcase() Swaps cases, lower case becomes upper case and vice versa
zfill() Fills the string with a specified number of 0 values at the beginning