KnowledgeBoat Logo
|
OPEN IN APP

Part II: AI — Chapter 4.4

Strings in Python

Class 10 - Exploring Robotics & AI



Multiple Choice Questions

Question 1

What does the given picture depict?

What does the given picture depict. Strings in Python, APC ICSE Robotics & Artificial Intelligence Solutions Class 10.
  1. String representation
  2. List representation
  3. Tuple representation
  4. None of these

Answer

String representation

Reason — The given word "ROBOT" is enclosed in double quotes (" "), which indicates that it is a string in Python. Also, each character is shown with forward and backward indexing. Since strings are written within quotes and support two-way indexing, the picture depicts a string representation.

Question 2

Which of the following operator can be used to replicate a string?

  1. //
  2. *
  3. **
  4. x

Answer

*

Reason — The (*) operator is used for string replication in Python. When a string is multiplied by a number, it repeats the string that many times (e.g., "Python" * 2 produces "PythonPython").

Question 3

Given: st = "keyboard"

What will be the correct output of print(st[-1])?

  1. k
  2. d
  3. rd
  4. ke

Answer

d

Reason — In Python strings, backward indexing starts from -1 for the last character. Since st = "keyboard", the index -1 refers to the last character of the string, which is d.

Question 4

Which of the following is the correct syntax to find the length of a string st?

  1. string.st()
  2. st.len()
  3. string.len()
  4. len(st)

Answer

len(st)

Reason — The len() function is used to find the number of characters present in a string. The correct syntax is len(st).

Question 5

Given: st = "ROBOTICS"

What will be the forward index of st[-3]?

  1. 5
  2. 3
  3. 7
  4. 8

Answer

5

Reason — In the string st = "ROBOTICS", backward indexing starts from -1 at the last character. Counting from the end: -1 = S, -2 = C, -3 = I. The character at st[-3] is 'I'. In forward indexing, the positions are: R(0), O(1), B(2), O(3), T(4), I(5), C(6), S(7). Thus, the forward index of 'I' is 5.

Question 6

Given: wd = "Virus"

Which of the following index will return its last character?

  1. len(wd) - 1
  2. len(wd) - 0
  3. len(wd) - 2
  4. len(wd) + 1

Answer

len(wd) - 1

Reason — In Python, forward indexing starts from 0, so the last character of a string is always at index length - 1. For wd = "Virus", len(wd) gives 5, and the last character is at index 5 - 1 = 4. Therefore, len(wd) - 1 will return the last character.

State True or False

Question 1

State whether the following statements are True or False:

  1. The (+) operator is used to concatenate two or more strings.
  2. In Python, a character literal and a string literal are treated differently.
  3. The return type of st.upper() method is either True or False.
  4. All relational operators are used for comparing strings.
  5. The output of len("ICSE 9") is 5.
  6. The find() function returns -1, if there is no occurrence of substring in the given string.
  7. The statement "ICSE" ** 2 is issued to replicate a string twice.
  8. The print(word[: : -1]) displays the word in a reversed order.

Answer

  1. True
  2. False
    Corrected Statement: In Python, a character literal and a string literal are treated the same.
  3. False
    Corrected Statement: The return type of st.upper() method is a string.
  4. True
  5. False
    Corrected Statement: The output of len("ICSE 9") is 6.
  6. True
  7. False
    Corrected Statement: The statement "ICSE" * 2 is used to replicate a string twice.
  8. True

Mention String Built-in Function

Question 1

Mention an appropriate string built-in function for the following tasks.

  1. To convert all the letters of a string to lowercase
  2. To check whether all the letters of a string are in uppercase or not
  3. To find the occurrence of a substring in a given string
  4. To represent a string in the form of a list
  5. To alter a character by another character in a given string
  6. To return the frequency of a substring in a given string

Answer

  1. lower()
  2. isupper()
  3. find()
  4. split()
  5. replace()
  6. count()

Assertion and Reason Based Question

Question 1

Assertion (A): Traversing is the process of accessing each and every character of a string with respect to their indices.

Reason (R): Accessing of characters can be performed by using forward indices or backward indices.

Based on the above assertion and reasoning, pick an appropriate statement from the options given below:

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true and R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.
  5. Both A and R are false.

Answer

Both A and R are true and R is the correct explanation of A.

Reason — Traversing means accessing each and every character of a string with respect to their indices. Since characters of a string can be accessed using forward or backward indices, the Reason correctly explains the Assertion.

Application Based Question

Question 1

There are various string built-in functions to perform several tasks on string manipulations. The following are some functions stated for the same purpose. Read the statements and name the functions used to perform these tasks:

(a) To find the length of a string
(b) To check whether all the letters in the string are letters or not
(c) To convert the string into uppercase letters
(d) To convert the string into sentence case (i.e., First letter of the first word of the string into uppercase whereas, rest are in lowercase).

Answer

(a) len()

(b) isalpha()

(c) upper()

(d) capitalize()

Programs on Strings

Question 1

Write a program to input a sentence. Find and display the number of words present in the sentence. Assume that the sentence neither include any digit nor a special character.

Sample Input : Python is one of the high level languages
Sample Output: Number of words = 8

Solution
st = input("Enter a sentence: ")
words = st.split()
count = len(words)
print("Number of words =", count)
Output
Enter a sentence: Python is one of the high level languages
Number of words = 8

Enter a sentence: Honesty is the best policy
Number of words = 5

Question 2

Write a program in Python to accept a name (containing three words) and display only the initials (i.e., first letter of each word).

Sample Input : LAL KRISHNA ADVANI
Sample output : L K A

Solution
name = input("Enter a name (three words only): ")
words = name.split()
if len(words) == 3:
    print(words[0][0], words[1][0], words[2][0])
else:
    print("Please enter exactly three words.")
Output
Enter a name (three words only): LAL KRISHNA ADVANI
L K A

Question 3

Write a Python program to input a string in lowercase. Replace the letter 'e' with '*' in the given string. Display the new string.

Sample Input : computer science
Sample Output: comput*r sci*nc*

Solution
st = input("Enter a string: ")
if st.islower():
    new_st = st.replace('e', '*')
    print("New string:", new_st)
else:
    print("Please enter the string in lowercase only.")
Output
Enter a string: computer science
New string: comput*r sci*nc*

Question 4

Write a program to accept a sentence. Display the sentence in reverse order of its words.

Sample Input : Computer is Fun
Sample Output : Fun is Computer

Solution
st = input("Enter a sentence: ")
words = st.split()
words.reverse()

new_st = " ".join(words)
print(new_st)
Output
Enter a sentence: Computer is Fun
Fun is Computer

Question 5

Consider the sentence as given below:

Blue bottle is in Blue bag lying on Blue carpet

Write a program to assign the given sentence in a string variable. Replace the word Blue with Red at all its occurrences. Display the new string as shown below:

Red bottle is in Red bag lying on Red carpet

Solution
st = "Blue bottle is in Blue bag lying on Blue carpet"
new_st = st.replace("Blue", "Red")

print(new_st)
Output
Red bottle is in Red bag lying on Red carpet

Answer the Following Questions

Question 1

What is meant by a string literal?

Answer

A string literal is defined as a collection of one or more characters (alphanumeric characters) enclosed within single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """).

Question 2(a)

Distinguish between the following functions lower( ) and upper( ).

Answer

lower()upper()
Converts all uppercase letters of a given string into lowercase letters.Converts all lowercase letters of a given string into uppercase letters.
Returns a new string in lowercase.Returns a new string in uppercase.

Question 2(b)

Distinguish between the following functions startswith( ) and endswith( ).

Answer

startswith()endswith()
Checks whether a string begins with the specified substring (prefix).Checks whether a string ends with the specified substring (suffix).
Returns True if the substring is found at the beginning, otherwise False.Returns True if the substring is found at the end, otherwise False.

Question 3

How will you slice a string using alternate characters? Explain with the help syntax.

Answer

A string can be sliced using alternate characters by specifying a step value in the slicing syntax. The step value decides the interval at which characters are selected.

Syntax: string_variable[first_index : last_index : step_value]

If the step value is 2, characters are selected by skipping one character each time.

For example:

wd = "ROBOTICS"
print(wd[0:8:2])

Output:

RBTC

Question 4

What are the various types of operators are used to compare strings in Python?

Answer

The relational operators used to compare strings in Python are <, >, <=, >=, ==, and !=.

Question 5

What are the ranges of ASCII codes for uppercase and lowercase letters?

Answer

The ASCII codes of uppercase letters range from 65 to 90, and the ASCII codes of lowercase letters range from 97 to 122.

Question 6

What is the result of comparison 'A' > 'a'? Justify your answer.

Answer

The result of the comparison 'A' > 'a' is False.

This is because strings are compared based on their ASCII values. The ASCII value of uppercase 'A' is 65, whereas the ASCII value of lowercase 'a' is 97. Since 65 is less than 97, the expression 'A' > 'a' evaluates to False.

Question 7

What is meant by traversing a string?

Answer

Traversing a string means accessing each and every character of a string one by one with respect to their indices (forward or backward) to perform some operation.

Question 8

Will "3" * "Hello!" perform replication? If no then why? Justify.

Answer

No, "3" * "Hello!" will not perform replication.

String replication is done by multiplying a string with an integer using the * operator, such as "Hello!" * 3. In the given expression, both "3" and "Hello!" are strings. Since multiplication of two strings is not valid in Python, it will result in an error.

Question 9(a)

Describe the purpose of the string method string.upper().

Answer

The string.upper() method converts all the lowercase letters of a given string into uppercase letters, ignoring other characters. If any letter is already in uppercase, or if a character is a digit or special symbol, it remains the same.

Question 9(b)

Describe the purpose of the string method string.isupper().

Answer

The string.isupper() method checks whether the given string comprises all uppercase letters or not. It returns True, if all the letters are in uppercase, otherwise False.

Question 9(c)

Describe the purpose of the string method string.captalize().

Answer

The string.capitalize() method is used to convert a string into sentence case. It converts the first letter of the string into uppercase and the rest of the characters into lowercase. If the first letter of the string is already in uppercase, then it remains the same.

Question 9(d)

Describe the purpose of the string method string.isalnum().

Answer

The string.isalnum() method checks whether the characters available in the given string are alphanumeric, that is, letters or digits. It returns True, if the characters comprise letters or digits, otherwise False.

Question 9(e)

Describe the purpose of the string method len(string).

Answer

The len(string) method returns the number of characters present in the string. It also counts the blank spaces present in the string.

Question 9(f)

Describe the purpose of the string method string.find().

Answer

The string.find() method determines whether the substring used as an argument occurs in the given string or not. It returns the index of the substring if found, otherwise it returns -1.

PrevNext