Please refer to String in Python Class 11 Computer Science Notes and important questions below. The Class 11 Computer Science Chapter wise notes have been prepared based on the latest syllabus issued for the current academic year by CBSE. Students should revise these notes and go through important Class 11 Computer Science examination questions given below to obtain better marks in exams
String in Python Class 11 Computer Science Notes and Questions
The below Class 11 String in Python notes have been designed by expert Computer Science teachers. These will help you a lot to understand all the important topics given in your NCERT Class 11 Computer Science textbook. Refer to Chapter 6 String in Python Notes below which have been designed as per the latest syllabus issued by CBSE and will be very useful for upcoming examinations to help clear your concepts and get better marks in examinations.
6.1 Introduction: Definition: Sequence of characters enclosed in single, double or triple quotation marks.
Basics of String:
- 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.)
- 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.
str = “kendriya”
str = ‘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= ‘ ‘)
6.3 String Operators:
a. Basic Operators (+, )
b. Membership Operators ( in, not in)
c. Comparison Operators (==, !=, <, <=, >, >=)
a. Basic Operators: There are two basic operators of strings:
i. String concatenation Operator (+)
ii. String repetition Operator (*)
i. String concatenation Operator: The + operator creates a new string by joining the two operand strings.
Note: You cannot concate numbers and strings as operands with + operator.
7+’4’ # unsupported operand type(s) for +: ‘int’ and ‘str’
It is invalid and generates an error.
ii. String repetition Operator: It is also known as String replication operator. It requires two types of operands- a string and an integer number.
”you” * 3
Note:You cannot have strings as n=both the operands with * operator.
”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
“ken” in “Kendriya Vidyalaya”
“Ken” in “Kendriya Vidyalaya”
“ya V” in “Kendriya Vidyalaya”
“8765” not in “9876543”
c. Comparison Operators: These operators compare two strings character by character according to their ASCII value.
‘ABC'<‘abc’ True ‘abcd’>’aBcD’
6.4 Finding the Ordinal or Unicode value of a character:
Program: Write a program to display ASCII code of a character and vice versa.
choice=int(input(“Press-1 to find the ordinal value \n Press-2 to find a character of a value\n”))
ch=input(“Enter a character : “)
val=int(input(“Enter an integer value: “))
print(“You entered wrong choice”)
print(“Do you want to continue? Y/N”)
if option==’y’ or option==’Y’:
6.5 Slice operator with Strings:
The slice operator slices a string using a range of indices.
where start and end are integer indices. It returns a string from the index start to end-1.
‘ ‘ #returns empty string
str[:14] # Missing index before colon is considered as 0.
str[0:] # Missing index after colon is considered as 14. (length of string)
str[:4]+str[4:] #for any index str[:n]+str[n:] returns original string
(‘ucture’, ‘data str’)
Slice operator with step index:
Slice operator with strings may have third index. Which is known as step. It is optional.
str[: : -1] # reverses a string
Interesting Fact: Index out of bounds causes error with strings but slicing a string outside the index does not cause an error.
IndexError: string index out of range
str[14:20] # both indices are outside the bounds
‘ ‘ # returns empty string
Reason: When you use an index, you are accessing a particular character of a string, thus the index must be valid and out of bounds index causes an error as there is no character to return from the given index.
But slicing always returns a substring or empty string, which is valid sequence.
6.6 Built-in functions of string:
s3 = ‘4567’
s4 = ‘ ‘
Programs related to Strings:
- Write a program that takes a string with multiple words and then capitalize the first letter of each word and forms a new string out of it.
s1=input(“Enter a string : “)
s2=”” #empty string
elif (s1[a]==’ ‘and s1[a+1]!=”):
print(“Original string : “, s1)
print(“Capitalized wrds string: “, s2)
- Write a program that reads a string and checks whether it is a palindrome string or not.
str=input(“Enter a string : “)
https://pythonschoolkvs.wordpress.com/ Page 60
for i in range(mid):
print(“String is not palindrome”)
print(“String is palindrome”)
- Write a program to convert lowercase alphabet into uppercase and vice versa.
choice=int(input(“Press-1 to convert in lowercase\n Press-2 to convert in uppercase\n”))
str=input(“Enter a string: “)
print(“Invalid choice entered”)