You are on page 1of 5

Chapter-10

String Manipulation
String Manipulation: String is a sequence of characters, which is enclosed between either
single('') or double quotes(""), python treats both single and double quotes same.

 Basic String Operators:


1. String Concatenation Operator ‘+’ :
a=”Tea”
b=”pot”
print(a+b)
print(“Tea” + “pot”)

Output:
Teapot
Teapot

2. String Replication Operator ‘*’ :


a=”Teapot”
print(a*3)
print(“123” * 2)
print(4 * “Teapot”)

Output:
TeapotTeapotTeapot
123123
TeapotTeapotTeapotTeapot

 String Membership Operators:


1. ‘in’ and ‘not in’ Operator :
a=”helping hand”
sub1=”hand”
sub2=”HAND”
sub1 in a
sub2 in a
sub2 not in a

Output:
True
False
True
String Slices : Part of string containing some contiguous characters from the string.

0 1 2 3 4 5
a= P Y T H O N
-6 -5 -4 -3 -2 -1
print( a[0 : 5] )
print( a[2 : 6] )
print( a[ : 3] )
print( a[-4 : -1] )
print( a[1 : 6 : 2] )

Output:
PYTHO
THON
PYT
THO
YHN

Format Symbol (%) :


%s : String conversion via str( ) prior to formatting
%d : Signed decimal integer
%o : Octal integer
%x : Hexa decimal integer
%f : Floating point real number
%c : Character

Ex. a=input("My Subject is %s and class is %d " % ('Computer Science', 11))


print(a)

Triple Quotes: It is used to create string with multiple lines.

Str1 = “””This course will introduce the learner to text


mining and text manipulation basics. The course
begins with an understanding of how text is handled by python”””
Built-in String Manipulation Methods/Functions:

a=”aims international school”


b=”I LOVE PYTHON”
c=”Class11”
d="ComputerScience"
e=”1234567890”
f=” ”
g=” String Functions ”

No. Syntax Description Example

1 string.capitalize( ) Capitalize the first character of a.capitalize( )


string. Aims international school
2 string.title( ) Capitalize the first character of a.title( )
each word and all remaining Aims International School
letters are in lowercase.
3 string.lower( ) String convert into lowercase. b.lower( )
i love python
4 string.upper( ) String convert into Uppercase. a.upper( )
AIMS INTERNATIONAL
SCHOOL
5 len(a ) Returns the length of its len(a)
argument string. 25
6 string.count(sub,start,end) Count the occurrences of sub a.count("n")
string in the whole string. 3
a.count("n",6,13)
2
7 string.find(sub,start,end) Find the lowest index position a.find("national")
of substring in the whole string. 10
Returns -1 if substring is not a.find("national",6,13)
found. -1
8 string.index(sub,start,end) Returned the index of 1st a.index("national",6,23)
occurrence of substring in the 10
whole string. Raised Error if a.index("national",6,13)
substring is not found. ERROR
9 string.isalnum( ) Return True if the characters in c.isalnum( )
the string are alphanumeric True
(alphabets or numbers) and a.isalnum( )
there is at least one character, False
False otherwise.
10 string.isalpha( ) Return True if the characters in c.isalpha( )
the string is alphabetic and False
there is at least one character, d.isalpha( )
False otherwise. True

11 string.isdigit( ) Return True if the characters in c.isdigit( )


the string is digits and there is False
at least one character, False e.isdigit( )
otherwise. True

12 string.islower( ) Return True if the string is in a.islower( )


lowercase, otherwise False. True
b.islower( )
False
13 string.isupper( ) Return True if the string is in a.isupper( )
uppercase, otherwise False. False
b.isupper( )
True
14 string.isspace( ) Return True if there are only a.isspace( )
whitespace characters in the False
string , otherwise False. f.isspace( )
True
15 string.istitle( ) Return True if the string is in a.istitle( )
title case, otherwise False. False
c.istitle( )
True
16 string.lstrip( ) Returns a copy of the string g.lstrip( )
with leading white-spaces 'String Functions '
removed.
17 string.rstrip( ) Returns a copy of the string g.rstrip( )
with trailing white-spaces ' String Functions'
removed.
18 string.strip( ) Returns a copy of the string g.strip( )
with leading and trailing white- 'String Functions'
spaces removed.
19 string.startswith(sub) Returns True if string starts a.startswith("school")
with the substring, otherwise False
returns False. a.startswith("aims")
True
20 string.endswith(sub) Returns True if string ends with a.endswith("school")
the substring, otherwise True
returns False. a.endswith("aims")
False
21 string.replace(old,new) Returns a copy of the string b.replace("PYTHON","PR
with all occurrences of OGRAMMING")
substring old replaced by new I LOVE PROGRAMMING
string.
22 string.join(string iterable) Join a string or character after "-".join(c)
each member of the string C-l-a-s-s-1-1
iterator. "**".join(c)
C**l**a**s**s**1**1
23 string.split(str/char) Splits a string, based on the a.split("n")
given str/char and return a list ['aims i', 'ter', 'atio', 'al
containing split strings as school']
members.
24 string.partition(separator) Splits the string at the first d.partition("ter")
occurrence of separator, and ('Compu', 'ter', 'Science')
returns a tuple containing three
items as string.

Write a program for the following (in your notebook):

(1) WAP to calculate the number of digits and letters in a string.


(2) WAP to print every character of a string entered by user in a new line using loop.
(3) WAP to find the length of any String without using len( ) function.
(4) WAP to find the first and last occurrence of letter ‘o’ in any string(print index positions).
(5) WAP that takes your full name as input and displays the abbreviations of the first and
middle names except the last name which is displayed as it is. For example, if your name
is Ram Kumar Gupta, then the output should be R. K. Gupta.
(6) WAP to check if the two strings entered by user are anagrams or not. Two words are said
to be anagrams if the letters of one word can be rearranged to form the other word. For
example, STUDY and DUSTY are anagrams of each other.

You might also like