0% found this document useful (0 votes)
11 views21 pages

Chapter 5

Chapter 5 pspp

Uploaded by

shanthini.j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views21 pages

Chapter 5

Chapter 5 pspp

Uploaded by

shanthini.j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Problem Solving and

Programming with

Python
Reema Thareja
Chapter 5 : Strings

© Oxford University Press 2019. All rights reserved.


Strings
• Python treats strings as contiguous series of characters delimited by single, double or even triple quotes.
Python has a built-in string class named "str" that has many useful features. We can simultaneously declare
and define a string by creating a variable of string type. This can be done in several ways which are as follows:

• name = "India" graduate = 'N' country = name nationality = str("Indian")

• Indexing: Individual characters in a string are accessed using the subscript ([ ]) operator. The expression in
brackets is called an index. The index specifies a member of an ordered set and in this case it specifies the
character we want to access from the given set of characters in the string.

• The index of the first character is 0 and that of the last character is n-1 where n is the number of characters in
the string. If you try to exceed the bounds (below 0 or above n-1), then an error is raised.

© Oxford University Press 2019. All rights reserved. 3


Strings

• Traversing a String: A string can be traversed by accessing character(s) from one index to another. For
example, the following program uses indexing to traverse a string from first character to the last.
• Example:

© Oxford University Press 2019. All rights reserved. 4


Concatenating, Appending and Multiplying Strings

• Examples:

© Oxford University Press 2019. All rights reserved. 5


Strings are Immutable

• Python strings are immutable which means that once created they cannot be changed. Whenever you try
to modify an existing string variable, a new string is created.
• Example:

© Oxford University Press 2019. All rights reserved. 6


String Formatting Operator
The % operator takes a format string on the left (that has %d, %s, etc) and the corresponding values in a tuple (will be
discussed in subsequent chapter) on the right. The format operator, % allow users to construct strings, replacing parts of
the strings with the data stored in variables. The syntax for the string formatting operation is:

"<Format>" % (<Values>)

Example:

© Oxford University Press 2019. All rights reserved. 7


Built-in String Methods and Functions

© Oxford University Press 2019. All rights reserved. 8


Built-in String Methods and Functions

© Oxford University Press 2019. All rights reserved. 9


Built-in String Methods and Functions

© Oxford University Press 2019. All rights reserved. 10


Slice Operation

A substring of a string is called a slice. The slice operation is used to refer to sub-parts of sequences and strings. You can
take subset of string from original string by using [ ] operator also known as slicing operator.

Example:

© Oxford University Press 2019. All rights reserved. 11


Specifying Stride while Slicing Strings

In the slice operation, you can specify a third argument as the stride, which refers to the number of characters to move
forward after the first character is retrieved from the string. By default the value of stride is 1, so in all the above
examples where he had not specified the stride, it used the value of 1 which means that every character between two
index numbers is retrieved.

Example:

© Oxford University Press 2019. All rights reserved. 12


ord() and chr() Functions

ord() function returns the ASCII code of the character and chr() function returns character represented by a ASCII
number.
Examples:

in and not in Operators

in and not in operators can be used with strings to determine whether a string is present in another string. Therefore, the in and not in
operator are also known as membership operators.

Examples:

© Oxford University Press 2019. All rights reserved. 13


Comparing Strings

© Oxford University Press 2019. All rights reserved. 14


Iterating String

• String is a sequence type (sequence of characters). You can iterate through the string using for loop.

• Examples:

© Oxford University Press 2019. All rights reserved. 15


The String Module

The string module consist of a number of useful constants, classes and functions (some of which are
deprecated). These functions are used to manipulate strings.

Examples:

© Oxford University Press 2019. All rights reserved. 16


Working with Constants in String Module

You can use the constants defined in the string module along with the find function to classify characters.
For example, if find(lowercase, ch) returns a value except -1, then it means that ch must be a lowercase
character. An alternate way to do the same job is to use the in operator or even the comparison operation.

Examples:

© Oxford University Press 2019. All rights reserved. 17


Metacharacters in Regular Expression

© Oxford University Press 2019. All rights reserved. 18


Metacharacters in Regular Expression

© Oxford University Press 2019. All rights reserved. 19


Character Classes

When we put the characters to be matched inside square brackets, we call it a character class. For example,
[aeiou] defines a character class that has a vowel character.

Examples:

© Oxford University Press 2019. All rights reserved. 20


Groups
A group is created by surrounding a part of the regular expression with parentheses. You can even give
group as an argument to the metacharacters such as * and ?.

Example:

The content of groups in a match can be accessed by using the group() function. For example,
• group(0) or group() returns the whole match.
• group(n), where n is greater than 0, returns the nth group from the left.
• group() returns all groups up from 1.

© Oxford University Press 2019. All rights reserved. 21

You might also like