0% found this document useful (0 votes)
21 views5 pages

Worksheet Exercise On Strings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Worksheet Exercise On Strings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Length of String
python
Copy code
>>> len("Computer Science") # Length of "Computer Science"
# Answer: 16

2. Predict the Following

Given:

python
Copy code
str = "Pithon"

(a) str[-2]
Answer: 'o'

(b) str[0] + str[-1]


Answer: 'Pn'

(c) str[10]
Answer: Error (index out of range)

(d) str[1] = 'y'


Answer: Error (strings are immutable, cannot assign to string index)

3. String Indexing with s = "Python World"

python
Copy code
s = "Python World"

(a) s.index('y')
Answer: 1

(b) s.index('y', 0)
Answer: 1

(c) s.index(' ', 2, 8)


Answer: 6

(d) s.index('l', 3, 10)


Answer: 9

4. Finding Substrings with str1 = "twinkle twinkle little star"


python
Copy code
str1 = "twinkle twinkle little star"

(a) str1.find('twinkle')
Answer: 0

(b) str1.find('twinkle', 2)
Answer: 8

(c) str1.find('twinkle', 9, 13)


Answer: -1 (not found within the range)

5. String Comparisons

(a) "xyz" == "xyz"


Answer: True

(b) "xyz" == "wxyz"


Answer: False

(c) "xyz" > "XYZ"


Answer: True (lowercase letters are greater than uppercase in ASCII)

(d) "XYZ" != "wxyz"


Answer: True

6. String Slicing with S = "python program"; P = "012345"

python
Copy code
S = "python program"
P = "012345"

(a) S[5]
Answer: 'n'

(b) S[5:]
Answer: 'n program'

(c) S[:5]
Answer: 'pytho'

(d) S[0:5]
Answer: 'pytho'
(e) S[::2]
Answer: 'pto rgam'

(f) S[:10:2]
Answer: 'pto r'

(g) S[::-1]
Answer: 'margorp nohtyp'

(h) S[-1:-10:-2]
Answer: 'mropn'

(i) S[8:-3:3]
Answer: 'oa'

(j) S[-3:8:1]
Answer: '' (no output since slicing in the reverse direction with positive step results in an
empty string)

(k) print(S[0], '-', S[0:5], '*', S[-1:-8:-2])


Answer: p - pytho * mrop

(l) P[0:5] + '$' + S[-1:0:2]


Answer: '01234$' (since S[-1:0:2] is empty in this case)

7. Membership Operators
python
Copy code
ch = "Chennai Training Institute for Education"

(a) "chennai" in ch
Answer: False (case-sensitive)

(b) "Ch" in ch
Answer: True

(c) "Tute" not in ch


Answer: True

8. Python Code to Reverse a String


python
Copy code
s = "Example"
reversed_s = s[::-1]
# Answer: "elpmaxE"

9. Predict the Output of the Python Code

(a)

python
Copy code
S = 'computer'
R = ''
for i in S:
R = R + i
print(S + R)
# Answer: 'computercomputer'

(b)

python
Copy code
S = 'computer'
R = ''
for i in S:
R = i + R
print(S + R)
# Answer: 'computerretupmoc'

(c)

python
Copy code
S = 'computer'
R = '#'
for i in S:
R = R + i
print(S + R)
# Answer: 'computer#computer'

(d)

python
Copy code
S = 'computer'
R = '#'
for i in S:
R = i + R
print(S + R)
# Answer: 'computerretupmoc#'

These answers cover the exercises based on string manipulation, indexing, slicing, and
membership operations as found in the document. Let me know if you'd like further explanations
for any of these!

You might also like