0% found this document useful (0 votes)
5 views

Strings

Uploaded by

hemlatasahu0103
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Strings

Uploaded by

hemlatasahu0103
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

STRINGS

INTRODUCTION

• Python strings are characters enclosed in quotes – single, double or triple


>>> str1 = ‘Hello World’
>>> str2 = “Hello World”
>>> str3 = “““Hello World”””
• Values stored, can be extended to multiple lines, using triple quotes (either ‘‘‘ ’’’ or “““ ”””)
• An empty string has 0 characters (just a pair of quotation marks – NO SPACE BETWEEN
THEM)
• Python strings are immutable i.e. the contents of the string cannot be changed after creation
>>> str1 [1] = ‘a’
TypeError: ‘str’ object doesn’t support item assignment
• Each character, part of the sequence has a unique position id / index. The indexes begin from
0 to length – 1 in the forward direction and – 1, to – length in the backward direction. This is
known as indexing (forward indexing and backward indexing).
• Individual characters can be accessed using their index (indexing)
Iterating through the elements is done using the for or while loop.
• The index should always be an integer
>>> m = ‘Amazing’
>>> m [1+3]
‘i’
>>> m [5-2]
‘z’
>>> m [1.5]
TypeError: string indices must be integers

STRING OPERATORS

• Basic Operators
◦ Arithmetic operators : + and * are the two basic operators
◦ Concatenation operator (+) :
▪ Creates a new string by joining the two operand strings
e.g. “tea” + “pot” = ‘teapot’
▪ Works with strings and strings separately for concatenation and addition respectively
and these cannot be combined
◦ Replication operator (*) :
▪ To use it with strings, a string and a number is required
e.g. “abc” * 2 = “abcabc”
▪ Works with strings and strings separately for replication and multiplication
respectively and these cannot be combined
• Membership Operators
◦ in, not in are the two operators which work for strings (and all sequence types)
▪ in – returns True if a character or substring exists in the given string, False otherwise
e.g. “a” in “heya”, “jap” in “japan” - gives True
▪ not in – returns True if a character or substring does not exist in the given string,
False otherwise
e.g. “jap” not in “Japan”, “123” not in “hello” - gives True
• Comparison Operators
◦ Python’s standard comparison / relational operators (<, >, ==, <=, >=, !=) apply to
strings also
◦ The comparison are based on standard character-by-character comparison rules for
Unicode (dictionary order)
◦ A built in function – ord ( ) takes in a single character and returns the corresponding
ordinal Unicode value
◦ The opposite of ord ( ) function is chr ( ) .
The ordinal function returns the ordinal value of a character while the latter takes the
ordinal value in integer form and returns the character corresponding to that ordinal
value.
>>> ord (‘A’)
65
>>> chr (65)
‘A’

STRING SLICING

• Refers to a part of the string


• The integer indicating the index of the last character is not considered, i.e.
variable_name [n : m], does not consider m and returns a string slice starting at n and
ending at m – 1
• If the begin-index is skipped, Python considers it as 0, by default and if the last value is
missing, Python considers the length of the string
• For a string,
word = “Amazing”
word [0 : 7] = ‘Amazing’
word [-5 : -1] = ‘azin’
word [ : 7] = ‘Amazing’
word [5 : ] = ‘ng’
• For any string, variable_name [: Z] + variable_name [Z :], gives the original string
• A third (optional) index (e.g. n) may be added. With that every nth element will be taken as
part of the slice. e.g.
word [1 : 6 : 2] = ‘mzn’
word [ : : –1] = ‘gnizama’
• Index out of bounds causes error with strings, but slicing a string outside the bounds does
not cause error e.g.
s = “Hello”
print (s [5]) – gives error, but
print(s [4 : 8]) – doesn’t give an error and prints output o (letter o, followed by “empty
string” in the next line)
◦ This is because, on using an index, a constituent character of the string, hence index
must be valid. But slicing returns a sub-sequence and empty sequence is a valid
sequence.

STRING FUNCTIONS AND METHODS

• Many built-in functions and methods for string manipulation are offered by Python
• Every string object created in Python, is an instance of String class
• String manipulation methods can be applied using the following syntax
<stringObject>.method name ( )
• A function is independent of Class, and of association with an object and can be called
directly, while a method is class dependent and associated with a class

You might also like