String Manipulation Python | Basics

 

NCERT Solutions for Class 11 Computer Science (Python)



 CH-10       STRING

      Manipulation



 
#Before that you have learnt a basic information about the computer how it works ,about computer system .how computer works and how it convert human language into machine language that is Binary language.

So Now we are going to study or learn about to make program to overcome the problem facing your user .By using your program ,the user will get his solution.

Before you have learnt how  to make a programs that what are keyswords,identifiers,operators etc.
Operators like +,-,*,/,etc.
Identifiers are identifiers those you assign any key
like a=12
here a is identifier through which you can identify 12 is where and = is an operator


Now Lets start 

What is String?

String is data type in which we can write a sentence in paranthesis (), by using ' ', " ",""" """,single quotes ,double quotes or triple quotes,so this is called string datatype.
So in string data type we can write any word ,number,or any special characters such as @,#,$,%,etc, in paranthesis.

Small program to understand about string 

a=('My name is abcd')

b=("My name is abcd")
c=("""My name is abcd""")

here values of a,b,c are written in string

Notes:Here we will find the index values of the single letter in the word and length of the string

Let take example to find index value 
a=("My name is abcd")

Here index value of M=0
y=1
d=14
because index value of space also count

if you want to find length of a string 
so for lenth of a string above written that is 15 
because index value start from 0 but length of string start with 1


OPERATORS OF STRING


1)Membership opertor  (in ,not in)
a=("My name is abcd")
print("My" in a)

>>>True



print("my" in a)

>>>False

print("ABCd" not in a)


OUTPUT
>>>True

print("abcd" not in a)

OUTPUT
>>>False

2.Addition operators 
a=("My name is abcd")
b=("Your name is bcds")
print(a+b)


OUTPUT
>>>My name is abcdYour name is bcds

3.Replication Operators 

a=("abcd")
print(a*2)

OUTPUT

>>>abcdabcd

a=("abcd")
b=("3")
print(a+b)
 

>>>abcd3

a=("abcd")
b=(3)
print(a+b)

here 3 it is not string data type before this program we have used 3 in a quotes

4.Relational Operators

a=("abcd")
b=("abcd")
print(a==b)

>>>True

here we check only first character of the string
if you will write that 
a=("abcd")
b=("Abcd")
print(a==b)

>>>False

 it is also a case sensitive is there is upper case character as well as lowercase character so it will differetiate.
similarily

a=("abcd")
b=("Abcd")
print(a>b)

>>>True
because ASCII value of a is 97
and A is 65





Now we will laern functions to make a program more easy

FUNCTIONS


1)len()
a=("Python is computer language")
print(len(a))

>>>27


2)capitalize()

if you want to make a first character of the string   in Upper case so you can use this function

a=("python is computer language")
print(a.capitalize())


>>>Python is computer language
#
3)find()
To find any character in a string so you can use find function

a=("python is computer language")
print(a.find("is"))

>>>7

because find() is only count character in a string it not count spaces in string in which whatever we have to find of that word it count its first character of the word as a number



4)islower()
If you want to a string is lower or not so you can use islower() function 

a=("python is computer language")
print(a.islower())

>>>True
5)isupper()
If you want or your user want to check given string is upper or not

"""
a=("python is computer language")
print(a.isupper())

>>>False

6)isalnum()
a=("pythoncomputerlanguage45")
print(a.isalnum())

>>>True 
a=("python computer language 45")
print(a.isalnum())

>>>False 

Because isalnum() function check first in string there is any number and also check there is no spaces in string.

7)Replace()
if you wnat to replace any word to put any other word so this function would be useful

a=("My name is abcd")
print(a.replace("abcd","Machine"))

>>>My name is Machine

8)count()
count() function is used to count the frequency of any word ,you can also a occurence any word without using this short cut   trick that is function

a=("My name is abcd   abcd ")
print(a.count("My"))

>>>1
a=("My name is abcd   abcd ")
print(a.count("bcds"))

>>>0

a=("My name is abcd   abcd ")
print(a.count("abcd"))

>>>2
9)max() and min()

These functions is used to find maximum and minimum value in the string


Testcases1:

a=("abcd")
print(max(a))
>>>d
Because the ASCII value value of d is 101


Testcases2:

a=("abcd   My")
print(max(a))


>>>y
Because ASCII value of y is 121


Testcases3:

a=("abcd")
print(min(a))

Output:
>>>a
Because ASCII value of a is 97


NOTE: if you want to find ASCII value of each  alphabet in uppercase as well as lower case so you can do this 

For thids you have to use ord() function 
and to find alphabet on that number so see below

Testcases
print(ord('a'))

>>>97

print(chr(65))

>>>A

Now if you have any query regarding this chapter so can comment to ask me.

Programmes /coding  of this chapter is given below link to click to get more program of string.

 



No comments:

Powered by Blogger.