string manipulation in python class 11

string manipulation in python class 11 notes

string manipulation in python class 11 notes

string manipulation in python class 11 notes

Traversing:-

Traversing refers to iterating through the elements of a string, one character at a time.
 

Q1.program to read a string and display it in reverse order- display one character per line.
do not create a reverse string, just display in reverse order.

ans. string1=input(“enter a string:”)
         print(“the”,string1,”in reverse order is:”)
         length=len(string1)
         for a in range(-1,(-length-1),-1):
                  print(string[a])
 
sample run of above program is:
enter a string : python
the python in reverse order is :
n
o
h
t
y
p
 

Q2.program to rea a string and display it in form.

                            first character                 last character
                            second character           second last character
 1.                                 .                                        . 

 2.                                 .                                         .                              

ans.  string1=input(“enter a string:”)
         length=len(string1)
         i=0
         for a in range(-1,(-length-1),-1):
                   print(string[i],”/t”,string[a])
                   i+=1
 
sample run of above program is:
enter a string : python
p              n
y              o
t              h
h              t
o              y
n              p
 

String operators:-

(i)Basic operator:-

the two basic operator of string are: + and *
String concatenation operator +

python string concatenation 2021

“tea” + “pot”
will result into
‘teapot’
 
2+3 = 5
‘2’+’3′ = ’23’
‘2’+3 —->invalid
 
python string manipulation notes

 

String replication operator *

number*string or string*number
example:
3*”go!”
will return
‘go!go!go!’
 
 
python string manipulation notes

 

(ii) Membership operator

in and not in

 
in :- returns true if a character or a substring exists in the given string; false otherwise
 
not in :- return true if a character or a substring does not exists in the given string, false otherwise
 
now let’s have look at some examples:
 
1. “a” in “heya”                will give true
2. “jap” in “heya”             will give false
3. “jap” not in “japan”      will give false
4. “123” not in “hello”      will give true
 

(iii) Comparison operator:-

python’s standard comparison operators i.e., all relational operators(<,>=,>,>=,==,!=) apply to string also.
 
examples:
 1. “a” == “a”              will give true
 
2. “abc” == “abc”      will give  true
 
3. “a” != “abc”           will give true
 
4. “A” !=  “a”               will give true
 
python string manipulation notes
 
 
thus upper-case are considered smaller than the lower-case letters. for instance,
1. ‘a’ <  ‘A’                  will give   false
2. ‘ABC’  >  ‘AB’         will give  true
3. ‘abc'<= ‘ABCD’     will give   false
4. ‘abcd’  >  ‘abcD’   will give  true
 

Determining ordinal/Unicode value of single character:-

ord( ) :-

python offer built-in function ord() that takes a single character and returns the corresponding ordinal Unicode value.it is used as per following format:
 
ord(<single-character>)
example:
>>>ord(‘A’)
65
 

chr() :-

chr(<int>)
example:
chr(65)
‘A’
 

String slices:-

part of a string containing some contiguous character from the string.
example:
1. word = ‘amazing’
2. word[0:7]     will give    ‘amazing’
3. word[0:3]     will give    ‘ama’
4. word[2:5]     will give    ‘azi’
5. word[-5:-1]   will give    ‘azin’
6. word[:7]        will give ‘amazing’
7. word[3:]        will give ‘zing’
8. word[::-1]      will give  ‘gnizama’
9. string [::-1] is an easy way to reverse a string.

Q3. program that prints the following pattern without using any nested loop

            1.  #
            2.  # #
            3. # # #
            4. # # # # 
            5. # # # # #
 
ans.  string = ‘#’
         pattern = ” ”       # empty string
         for a in range(5):
         pattern+=string
         print(pattern)
 
sample run of the program is shown below:
              1. #
              2. # #
              3. # # #
              4. # # # # 
              5. # # # # #
 

String functions and methods :-

 

 

Read More :

string manipulation in python class 11 notes

You may also like...

3 Responses

  1. November 9, 2020

    […] STRING MANIPULATION […]

  2. February 5, 2021

    […] String manipulation in python in class 11 notes […]

  3. February 6, 2021

    […] string manipulation in python notes […]

Leave a Reply

Your email address will not be published. Required fields are marked *