0% found this document useful (0 votes)
7 views51 pages

Python String Manipulation Guide

Chapter 10 covers string manipulation in Python, defining strings as groups of characters enclosed in quotes and explaining indexing, accessing, and traversing strings. It discusses various string operators, including concatenation, replication, membership, and comparison operators, along with string functions and methods for manipulation. The chapter also introduces string slicing, ASCII value retrieval, and several built-in functions for string processing.

Uploaded by

mmahalakshmi94
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)
7 views51 pages

Python String Manipulation Guide

Chapter 10 covers string manipulation in Python, defining strings as groups of characters enclosed in quotes and explaining indexing, accessing, and traversing strings. It discusses various string operators, including concatenation, replication, membership, and comparison operators, along with string functions and methods for manipulation. The chapter also introduces string slicing, ASCII value retrieval, and several built-in functions for string processing.

Uploaded by

mmahalakshmi94
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

CHAPTER-10

STRING
MANIPULATION
What is string?
 String may be defined as a group of character
enclosed within the quotes.
 Quotes may be single, double or triple
quotes.
Example: ''Ram''
• Any python object enclosed within the
quotes are considered as strings even if
there are numbers.
INDEXING
 Each character in a string consist of
a number is known as Index.
 If the indexing of the strings begins
with 0 to length(1) from the left, is
known as Forward Indexing.
 If the indexing of the strings begins
from-1 to - length from the right, is
known as Backward Indexing.
INDEXING

FORWARD
INDEXING 0 1 2 3 4
I N D I A
-5 -4 -3 -2 -1
BACKWARD
INDEXING
LENGTH :5
ACCESSING ITEM OF
STRING
 The individual elements of the string can be
accessed using its index.
Syntax:
stringname[index]
Example:
x=india
x[0] x[-4]
i n
TRAVERSING A STRING
 Traversing is refers to iterating through the
elements of the string one character at the time.
TRAVERSING A STRING
IN REVERSE ORDER

Output:
Enter a string to traverse:INDIA
String after traversing:
A
I
D
N
I
OPERATORS IN STRING

 Operators are symbols that trigger some computation when


applied to variable, constant and other object.
 Different types of operators are,
 Concatenation Operator (+)
 Replication Operator (*)
 Membership Operator
 Comparison Operator
Concatenation Operator
 Concatenation refer to joining of string.

 It denoted by '+' symbol.


 This operator creates a new string by joining the
two operand string.
SYNTAX:
"string1"+"string2"
Example:
>>>"Computer"+" Science "
'Computer Science'
Concatenation Operator
 This Operators (+) work in
situation where both operands are
numbers and string.
Example:
>>>10+2
12
• Combining numbers and strings
with + operator result in error.
Example:
>>>"key" + 3
Error
Replication Operator
 Replication refer to displaying a string for a

given number of lines.

 It denoted by *.

 This operator returns a new string by

replicating a string for n number of times.

SYNTAX:
"string"*3
Example:
>>>" Cake"*3
Replication Operator
 This operator (*) works in situation where
both operands are numbers or one string and
the other a positive integer.
Example:
>>>3*2
6
 Using it with both operands of the string
type generates an error.
Example:
>>>"python"*"python"
Membership Operator
 These are the set of operators that

are used to check whether a

character or a group of character is

contained in a string or not.

 Different Types are

 In
IN Operator:
 This operator returns TRUE, if a character or

a substring is contained in a given string,

FALSE otherwise

Example:

>>>"i" in "Abi" >>> "bi" in "Abi"

True True

>>> "Ai" in "Abi"

False
NOT IN Operator:
 This operator returns TRUE, if a character or a

substring is not contained in a given string,

FALSE otherwise

Example:

>>>"p" not in "Abi" >>> "bi" not in

"Abi"

True False

>>> "Ai" not in "Abi"


Comparison Operator
 The relational operators are used in python

for string comparison.

 It includes >,<,>=,<=,==,!=.

 Comparison is donr character by character

based on their ASCII values.

Example:

>>> "Raj">" raj" >>>"Raj"<"Ragul"

False False
How to find the ASCII value of a
character?
 The ord( ) is used in order to find the ASCII

value of the character.


Example:
>>> ord('a') >>>ord ('C')
97 67

The chr( ) is used to find the character

corresponding to a ASCII value.


Example:
>>> chr(65) >>>chr(103)
What is string slicing?
 String slicing is refer to displaying only a

part of the entire string.

 Its done in python using a range of

indices.

SYNTAX:

<string_name>[l:u]

Python will return a string in the range


What is string slicing?

Example:
STRING
NAME : X I N D I A
INDICES 0 1 2 3 4
>>>x="INDIA"
>>>x[1:4]
'NDI'
• In a string slicing, the character at
the last index is not included in the
results.
What is string slicing?
Example:
STRING NAME :
X I N D I A
INDICES -5 -4 -3 -2 -1
>>>x="INDIA"
>>>x[-4:-1]
'NDIA'
>>>x[::-1]
'AIDNI'
STRING SLICING WITH STEP VALUE
• Step value is used in order to skip
certain character of the string while
slicing strings.

SYNTAX:

<string_name>[l:u:step value]

Example:
>>>x="INDIA"
>>>x[0:5:2]
'IDA'
STRING SLICING WITH STEP VALUE
STRING FUNCTION AND METHODS
len ( ):
 This function is used to find tge length of a
string.
 It counts the number of character of tge string.

SYNTAX:

len("string")

Example:
>>>len("INDIA")
5
>>>len("Hello world!)
12
capitalize ( )
 This function is used to return a copy
of the string with its first character
capitalized.

SYNTAX:

<string>.capitalize ( )

Example:
>>>x="hello"
[Link] ( )
count( )
 This function is used to return a number of times a
specified substring appears in a given string within the
range of indices.
 If the range is not specified it return the specified number
of item from the entire string.
SYNTAX:
Example:2
<string>.count(sub[,start[,end]]) >>>
'balloon'.count('ll',4,7)
Example:1 0
>>>
>>> 'balloon'.count('ll') 'balloon'.count('ll',1,5)
1 1
>>> 'balloon'.count('l') Example:3
>>> 'balloon'.count('ll',0)
2
1
>>> 'balloon'.count('ll',6)
0
find( )
 This function is used to return the lowest index in the
string where the substring sub is found within the slice
range of start and end.
SYNTAX:

<string>.find (sub[,start[,end]])

Example:

string='it goes as - ringa ringa roses'

sub='ringa‘

[Link](sub) [Link](sub,15,22)

13 -1

[Link](sub,15,22)
find ( )
index( )
 This function is used to return
lowest index where the specified
substring is found.
SYNTAX:

<string>.index (sub[,start[,end]])
index( )
title ( )
 This function is used to return a copy of string
with the first character of each word in upper
case if it is in lowercase.
 If the first character of the word is already in
uppercase, there is no change.
SYNTAX:

<string>.title ( )

Example:

>>>"welcome to [Link]( )
isalnum( )
 This function is used to test if all the character of
a string are alphanumeric or not.
 Alphanumeric means a character that is either an
alphabets or numbers.
 It return True if all the character are
alphanumeric,False otherwise.
SYNTAX: <string>.isalnum( )

Example:

>>>x="srv2024" x="srv@2024"

[Link]( ) [Link]( )
isalpha( )
 This function is used to test if all the characters
of the string consist of alphabet or not.
 It return True if all the characters are
alphabet,False otherwise.
SYNTAX: <string>.isalpha( )

Example:

>>>'india2024'.isalpha( )

False

>>>'India'.isalpha( )

True
isdigit( )
 This function is used to test if all the characters of
the string consist of digits or not.
 It return True if all the characters are digits,False
otherwise.
SYNTAX: <string>.isdigit( )

Example:

>>>'2024'.isdigit( ) >>>'20SRV'.isdigit( )

True False

>>>'20.24'.isdigit ( )

False
islower( )
 This function is used to test if all the characters of
the string consist of lowercase alphabets or not.
 It return True if all the alphabets are in
lowercase,False otherwise.
SYNTAX: <string>.islower( )

Example:

>>>'India'.islower( ) >>>'india'.islower( )

False True

>>>'20.24'.islower( )
isupper( )
 This function is used to test if all the characters of
the string consist of uppercase alphabets or not.
 It return True if all the alphabets are in
uppercase,False otherwise.
SYNTAX: <string>.isupper( )

Example:

>>>'India'.isupper( ) >>>'INDIA'.isupper( )

False True

>>>'20.24'.isalpha( )
isspace( )
 This function is used to test if all the
characters of the string consist of whitespace
character or not.
 It return True if all the character are white
space character,False otherwise.
SYNTAX: <string>.isspace( )

Example:

>>>' '.isupper( ) >>>' .

'.isupper( )
lower( )
 This function is used to convert all
uppercase characters of the string
into lowercase character.
SYNTAX: <string>.lower( )

Example:

>>>'INDiA '.lower( )

>>>'india2024'.lower()
upper( )
 This function is used to convert all
lowercase characters of the string
into uppercase character.
SYNTAX: <string>.upper( )

Example:

>>>'india'.upper( )

>>>'India'.upper()
startswith( )
 This function return True if the given string
starts with the substring passed as an argument,
False otherwise.

SYNTAX:
<string>.startswith('substring')

Example:
endswith( )
 This function return True if the given string
ends with the substring passed as an argument,
False otherwise.

SYNTAX:
<string>.endswith('substring')

Example:
replace( )
 This function is used to replace an old phase of
data with a new phrase of data.

SYNTAX:
<string>.replace(old_phrase ,new_pharse,count)

Where,
 old_pharse refers to part of original
data
 new_pharse refers to a part of new
data
 count is optional which specifies the
replace ( )
join( )
 This function is used to join a string or character
(s) after each character of the string passed as
argument.

SYNTAX:
<string/character to be joined>.join(<string >)

Example:
partition ( )
 This function is used in order to split a string
into three parts at the first occurrence of the
separator passed as argument.
 It return a tuple containing 3parts:
 The part before the separator
 The separator itself
 The part after the separator

SYNTAX:
<string>.partition("separator")
partition ( )
split ( )
 This function is used to split(separate) a string
based on the given substring.
 It returns a list containing the split strings as
members of the list.
lsplit ( )
 This function is used to remove leading
characters from the left side of a string as per
the argument passed to the functions.
 If no argument is passed to the function, leading
white space character are removed.
 It does not modify the original string but returns
a new string with the specifief leading characters
removed.
 SYNTAX:
<Original _string >.lstrip("leading character to be
THANK
YOU

You might also like