MENU

Chapter 12 Strings Solutions

Question - 11 : -
Describe the isdigit( ) method

Answer - 11 : -

The method isdigit( ) checks whether the string consists of digits only

Question - 12 : -
Why we use islower( ) method in python?

Answer - 12 : -

The method islower( ) checks whether all the case-based characters (letters) of the string are lowercase

Question - 13 : -
Describe the isspace( ) method

Answer - 13 : -

The method isspace( ) checks whether the string consists of whitespace.

Question - 14 : -
Write the output of the following code.
# !/usr/bin/python
str = “Waltons Technology….wow!!!”;
print “str.upper() : “str.upper()

Answer - 14 : -

str.upper() : Waltons Technology ….WOW!!!

Question - 15 : -
What do you mean by string in Python ?

Answer - 15 : -

Strings are amongst the most popular types in Python. We can create them simply by characters in quotes. Python treats single quotes the same as double quotes.
Creating strings is as simple as assigning a value to a variable. For example :
var1 = ‘Waltons Technology!’
var2 = “Python Programming”

Question - 16 : -
What is indexing in context to Python strings ? J Why is it also called two-way indexing ?

Answer - 16 : -

In Python strings, each individual character is ! given a location number, called “index” and this process is called “indexing”. Python allocates indices in two directions :

  1. in forward direction, the indexes are numbered as 0,1, 2,    length-1.
  2. in backward direction, the indexes are numbered as -1, -2, -3,…. length.
This is known as “two-way indexing”.

Question - 17 : -
What is a string slice ? How is it useful ?

Answer - 17 : -

A sub-part or a slice of a string, say s, can be obtained using s[n : m] where n and m are integers. Python returns all the characters at indices n, n+1, n+2,…. m-1.
For example,
‘Oswaal Books’ [1 : 4] will give ‘swa’

Question - 18 : -
How you can “update” an existing string ?

Answer - 18 : -

You can “update” an existing string by (re) assigning a variable to another string.
The new value can be related to its previous value or to a completely different string altogether.
Following is a simple example :
# !/usr/bin/python
var1 = ‘Hello World!’
print”Updated String:-“,var i[:6] + ‘Python’

Question - 19 : -
Define raw string with example.

Answer - 19 : -

Raw strings don’t treat the backslash as a special character at all. Every character you put into a raw string stays in the way you wrote it :
# !/usr/bin/python
print ‘C:\\nowhere’
When the above code is executed, it produces the following result :
C:\nowhere
Now let’s make use of raw string. We would put expression in r’expression’ as follows :
# !/usr/bin/python
print r’C:\\nowhere’
When the above code is executed, it produces the following result :
C:\\nowhere

Question - 20 : -
Explain Unicode String with example.

Answer - 20 : -

Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16- bit Unicode. This allows for a more varied set of characters, including special characters from most languages in the world.
Example…….
#!/usr/bin/python
print u’Hello, world!’

Free - Previous Years Question Papers
Any questions? Ask us!
×