What is String in Python?
Strings are contiguous series of characters enclosed in single or double quotes. Python doesn’t have any separate data type for characters so they are represented as a single character string.
For eg:
>>> s_name = “Amit” # s_name is a variable storing a string.
>>>s = ‘a’ #String can also be enclosed in single quotes
How to Create Strings in Python?
Creating strings: To create string enclose the character or sequence of character in Single or Double Quotes like shown below
>>> s_name = “Amit” #String enclosed in double quotes
>>>s = ‘a’ # String enclosed in Single Quotes
We can also use str() function to create string:
N = str () # This function will create an empty string
name = str(1234) # This will create a variable name which store a string “1234”
If we execute the following code:
>>>print(name)
Output will come
1234
Traversing Strings in Python:
Traversing a String: It means accessing all the elements of the string one by one using index value.
St = “PYTHON”
St[1] = Y | St[5] = N | St[-1] = N | St[-6] = P |
Iterating through string using for loop:
Traversing the strings in Python using for loop
Operation of Strings in Python
Membership Operator : there are two membership operators: 1. ‘in’ operator returns True if a substring is present in main string 2. ‘not in’ operator returns True if a substring is not present in main string. str1 = “Welcome” print(‘com’ in str1) print(‘W’ in str1) print(‘Wel’ not in str1) | OUTPUT True True False |
String Comparison : We can compare the string using relational/comparison operator like (>, <, >=, <=, ==, !=) print(“amit” == “Amit”) print(“amit” != “Amit”) print(“blog” > “z”) | OUTPUT False True False |
NOTE: Strings are immutable means that the content of the string cannot be changed
We can not change the character in the String
Inbuilt Functions of Strings in Python:
Function Name | Description | Code | Output |
split() | This function splits the string into a list of string on the basis of delimiter | str = ” I am Learning Python” print(str.split()) | [‘I’ , ‘am’ , ‘Learning’ , ‘Python’ ] |
upper() | This function converts the string into uppercase | str = ” I am Learning Python” print(str.upper()) | I AM LEARNING PYTHON |
lower() | This function converts the string into lowercase | str = ” I am Learning Python” print(str.lower()) | i am learning python |
replace() | This function replaces a substring from the main string. As strings are immutable so this function creates a new string | str = ” I am Learning Python” newstr=str.replace (“Learning”,”Doing”) print(newstr) | I am Doing Python |
find() | This function return the index of substring in a main string. | str = “I am Learning Python” print(str.find(‘am’)) | 2 |
len() | This function returns the length of the string. | str = “I am Learning Python” print(len(str)) | 20 |
strip() | This function is used to remove leading and trailing whitespace from the string. | str=” I am Learning Python” print(len(str)) strnew=str.strip() print(len(strnew)) | 25 20 |
count() | This function counts the number of occurrences of a substring in main string | str=”I am Learning Python” print(str.count(‘a’)) print(str.count(‘n’)) | 2 3 |
capitalize() | This function converts the first alphabet of the string to upper case and all other alphabets in small case | str=”I am Learning Python” print(str.capitalize()) | I am learning python |
index() | This function returns the lowest index of substring in a string. | str=”I am Learning Python” print(str.index(‘a’)) | 2 |
isalnum() | This function returns True if it’s made of alphanumeric characters only. If there is one space in a string, this function will return False. | str=”I am Learning Python” print(str.isalnum()) str=”IamLearningPython” print(str.isalnum()) | False (#it contain spaces) True |
isdigit() | This function returns True if all the characters in the string are digits otherwise False. | str=”12345678″ print(str.isdigit()) str=”12345678A” print(str.isdigit()) | True(#only digits) False(#contain alphabet) |
islower() | This function returns True if all the characters in the string are in lower case | str=”I am Learning Python” print(str.islower()) str=”i am learning python” print(str.islower()) | False(#contain upper case alphabet) True |
isupper() | This function returns True if all the characters in the string are in upper case | str=”I am Learning Python” print(str.isupper()) str=”I AM LEARNING PYTHON” print(str.isupper()) | False(#contain small case) True |
isspace() | This function returns True if all the characters in the string is whitespace. | str=”I am Learning Python” print(str.isspace()) str=” “ print(str.isspace()) str = “” print(str.isspace()) | False True (#contain only space) False |
istitle() | This function converts the given string in title case(first alphabet of each word is in upper case). | str=”i am learning python” print(str.title()) | I Am Learning Python |
swapcase() | This function converts the lowercase characters to upper case and vice-versa. | str=”I am Learning Python” print(str.swapcase()) | i AM lEARNING pYTHON |
Practice Questions
Q1. Write a program to count the frequency of a character in a string.
str=input("Enter any String") ch=input("Enter the character") print(str.count(ch))
Q2. Write a program to accept a string and return a string having first letter of each word in capital.
str=input("Enter any String") print(str.title())
Q3. Write a program to accept a string and display the following:
- Number of uppercase characters
- Numbers of lowercase characters
- Total number of alphabets
- Number of digits
str=input("Enter any String") u=0 L=0 d=0 l = len(str) for i in range(l): if str[i].isupper(): u = u+1 if str[i].islower(): L = L + 1 if str[i].isdigit(): d = d+1 print("Total Upper Case Characters are: ", u) print("Total Lower Case Characters are: ", L) print("Total Characters are: ", L + u) print("Total digits are: ", d)
Q4. Write a program to reverse a string.
str=input("Enter any String") print(str[::-1])
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series