Saturday , April 27 2024
Class XI & XII Computer Science: Python

String in Python: 11th Class Computer Science Chapter 06

Chapter Name: String in Python [Chapter 06]
Class: 11th
Subject: Computer Science

6.1 Introduction: String in Python

Definition – String in Python: Sequence of characters enclosed in single, double or triple quotation marks.

Basic of Strings:

  • Strings are immutable in python. It means it is unchangeable. At the same memory address, the new value cannot be stored.
  • Each character has its index or can be accessed using its index.
  • String in python has two-way index for each location. (0, 1, 2, ……. In the forward direction and -1, -2, -3, …….. in the backward direction.)

Example:

  • The index of string in forward direction starts from 0 and in backward direction starts from -1.
  • The size of string is total number of characters present in the string. (If there are n characters in the string, then last index in forward direction would be n-1 and last index in backward direction would be –n.)
  • String are stored each character in contiguous location.
  • The character assignment is not supported in string because strings are immutable

Example:

str=”kendriya”

str[2]=’y’        # it is invalid. Individual letter assignment not allowed in python

6.2 Traversing a String:

Access the elements of string, one character at a time.

str = “kendriya”
for ch in str :
print(ch, end= ‘ ‘)

Output:
kendriya

6.3 String Operators:

  • Basic Operators (+, *)
  • Membership Operators ( in, not in)
  • Comparison Operators (==, !=, <, <=, >, >=)

a) Basic Operators (+, *)

There are two basic operators of strings:

  1. String concatenation Operator (+)
  2. String repetition Operator (*)

1 ) String concatenation Operator: The + operator creates a new string by joining the two
operand strings.

Example:
>>>”Hello”+”Python”
‘HelloPython’
>>>’2’+’7’
’27’
>>>”Python”+”3.0”
‘Python3.0’

Note: You cannot concatenate numbers and strings as operands with + operator.
Example:
>>>7+’4’ # unsupported operand type(s) for +: ‘int’ and ‘str’
It is invalid and generates an error

2) String repetition Operator: It is also known as String replication operator. It requires
two types of operands- a string and an integer number.

Example:
>>>”you” * 3
‘youyouyou’
>>>3*”you”
‘youyouyou’

Note: You cannot have strings as n=both the operands with * operator.

Example:
>>>”you” * “you” # can’t multiply sequence by non-int of type ‘str’
It is invalid and generates an error.

b) Membership Operators:

in – Returns True if a character or a substring exists in the given string; otherwise False
not in – Returns True if a character or a substring does not exist in the given string; otherwise False

Example:
>>> “ken” in “Kendriya Vidyalaya”
False
>>> “Ken” in “Kendriya Vidyalaya”
True
>>>”ya V” in “Kendriya Vidyalaya”
True
>>>”8765″ not in “9876543”
False

c) Comparison Operators:

These operators compare two strings character by character according to their ASCII value.

Example:

>> ‘abc’>’abcD’
False
>>> ‘ABC'<‘abc’
True
>>> ‘abcd’>’aBcD’
True
>>> ‘aBcD'<=’abCd’
True

6.4 Finding the Ordinal or Unicode value of a character:

Example:
>>> ord(‘b’)
98
>>> chr(65)
‘A

Program: Write a program to display ASCII code of a character and vice versa.

var=True
while var:
choice=int(input(“Press-1 to find the ordinal value \n Press-2 to find a character of a value\n”))
if choice==1:
ch=input(“Enter a character : “)
print(ord(ch))
elif choice==2:
val=int(input(“Enter an integer value: “))
print(chr(val))
else:
print(“You entered wrong choice”)
print(“Do you want to continue? Y/N”)
option=input()
if option==’y’ or option==’Y’:
var=True
else:
var=False

6.5 Slice operator with Strings:

The slice operator slices a string using a range of indices.

Syntax:

string-name[start:end] where start and end are integer indices. It returns a string from the index start to end-1.

Example:
>>> str=”data structure”
>>> str[0:14] ‘data structure’
>>> str[0:6] ‘data s’
>>> str[2:7] ‘ta st’
>>> str[-13:-6] ‘ata str’
>>> str[-5:-11] ‘ ‘ #returns empty string
>>> str[:14] # Missing index before colon is considered as 0.
‘data structure’
>>> str[0:] # Missing index after colon is considered as 14. (length of string)
‘data structure’
>>> str[7:] ‘ructure’
>>> str[4:]+str[:4] ‘ structuredata’
>>> str[:4]+str[4:] #for any index str[:n]+str[n:] returns original string
‘data structure’
>>> str[8:]+str[:8] ‘ucturedata str’
>>> str[8:], str[:8] (‘ucture’, ‘data str’)

Slice operator with step index:

Slice operator with strings may have third index. Which is known as step. It is optional.

Syntax:
string-name[start:end:step]

Example:
>>> str=”data structure”
>>> str[2:9:2] ‘t tu’
>>> str[-11:-3:3] ‘atc’
>>> str[: : -1] # reverses a string
‘erutcurts atad’

6.6 Built-in functions of string in Python:

Example:
str=”data structure”
s1= “hello365”
s2= “python”
s3 = ‘4567’
s4 = ‘ ‘
s5= ‘comp34%@’

Check Also

Class XI & XII Computer Science: Python

Tuple in Python: 11th Class Computer Science Chapter 08

Chapter Name: Tuple in Python [Chapter 08] Class: 11th Subject: Computer Science 8.1 INTRODUCTION: Tuple …