Strings
Strings
▪ String and string operations, List- creating list, accessing, updating and
deleting elements from a list, basic list operations.
▪ Tuple- creating and accessing tuples in python, basic tuple operations.
▪ Dictionary, built in methods to access, update and delete dictionary values.
Set and basic operations on a set.
▪ Strings are one of the most popular data types in Python.
▪ Strings are created by enclosing various characters within quotes. Python does not
distinguish between single quotes and double quotes.
▪ Creating strings is very simple in Python
>>> var1 = ‘Hello Python!’
>>> var2 = ”Welcome to Python Programming!”
>>> var3 = ”””This is triple quoted string”””
>>> print var1
‘Hello Python’ # Output
>>> print var2
‘Welcome to Python Programming’ # Output
>>> print var3
‘This is triple quoted string’ # Output
▪ Strings are immutable. If you want to change an element of a string, you
have to create a new string
▪ Triple quoted strings can span to multiple lines.
▪ >>> var = “““Welcome # String written in multiple lines
to
Python
Programming”””
>>> prin(var)
Welcome # Output
to
Python
Programming
▪ Strings are made up of smaller pieces/characters. The data types that are
made up of smaller pieces are known as compound data types.
▪ Strings, in Python, can be used as a single data type, or, alternatively, can
be accessed in parts.
▪ This makes strings really useful and easier to handle in Python.
▪ In order to access a part of the string, a square bracket operator ([]) must be
used.
>>> var = "hello"
>>> letter = var[4]
>>> print letter
o # Output
▪ len is a built-in function in Python.
▪ When used with a string, len returns the length or the number of characters in the
string
>>> var = “Hello Python!”
>>> len(var)
13 # Output
▪ We can use negative indices for accessing the string from last
>>> var = “Hello world”
>>> last = var[-1]
>>> second_last = var[-2]
▪ >>> print last
▪ d # Output
▪ >>> print second_last
▪ l # Output
▪ A piece or subset of a string is known as slice.
▪ Slice operator is applied to a string with the use of square braces ([]).
▪ Operator [n:m] will give a substring which consists of letters between n and m
indices, including letter at index n but excluding that at m, i.e. letter from nth
index to (m-1)th index.
▪ Similarly, operator [n:m:s] will give a substring which consists of letters from
nth index to (m-1)th index, where s is called the step value, i.e. after letter at n,
that at n+s will be included, then n+2s, n+3s, etc
>>> var = ‘Hello Python’
>>> print var[0:4]
Hell # Output
▪ >>> print var[6:12]
▪ Python # Output
>>> alphabet = “abcdefghij”
>>> print alphabet[1:8:3]
beh # Output
▪ >>> print alphabet[1:8:2]
▪ bdfh # Output
▪ If we do not give any value for the index before the colon, i.e., n, then the slice will
start from the first element of the string. Similarly, if we do not give any value for the
index, i.e., m after the colon, the slice will extend to the end of the string.
▪ If we don’t give any value at both the sides of the colon, i.e., values for n and m are not
given then it will print the whole string.
▪ >>> var = ‘banana’
▪ >>> var[:4]
▪ ‘bana’ # Output
▪ >>> var[4:]
▪ ‘na’ # Output
▪ Traversal is a process in which we access all the elements of the string one
by one using some conditional statements such as for loop, while loop, etc.
▪ String traversal is an important pattern since there will be many situations
in different programs where we need to visit each element of the string and
do some operations, continuing till the end of the string.
▪ >>>var=‘banana’
OUTPUT
▪ >>> i=0
b
>>> while i < len(var): a
n
letter = var[i]
a
print (letter) n
i=i+1 a
▪ for char in var:
print char
▪ Each time in the -for loop, the next character in the string will be assigned
to the variable char.
▪ The loop halts when the last character is processed.
▪ String Concatenation is the technique of combining two strings.
▪ String Concatenation can be done using many ways.
▪ Using +,%,join operator etc
▪ Simplest method is to use + operator to add multiple strings together.
▪ However, the arguments must be a string.
▪ var1 = "Hello "
var2 = "World"
var3 = var1 + var2
print(var3)
‘Hello World’ #output
▪ The Concatenating strings with the “*” operator can create multiple
concatenated copies of same string.
▪ print(“hello ”* 3)
hello hello hello#output
▪ Python has a set of built-in methods that you can use on strings.
▪ All string methods returns new values. They do not change the original
isalnum() Returns True if all characters in the string are
string. alphanumeric
capitalize() Converts the first character to upper case isalpha() Returns True if all characters in the string are in
the alphabet
casefold() Converts string into lower case isascii() Returns True if all characters in the string are
center() Returns a centered string ascii characters
count() Returns the number of times a specified value isdecimal() Returns True if all characters in the string are
occurs in a string decimals
encode() Returns an encoded version of the string isdigit() Returns True if all characters in the string are
digits
endswith() Returns true if the string ends with the isidentifier() Returns True if the string is an identifier
specified value
islower() Returns True if all characters in the string are
expandtabs() Sets the tab size of the string lower case
find() Searches the string for a specified value and isnumeric() Returns True if all characters in the string are
returns the position of where it was found numeric
isprintable() Returns True if all characters in the string are
format() Formats specified values in a string printable
isspace() Returns True if all characters in the string are
whitespaces