Loading samples...
Loading samples...
py-sample-8
Advanced string operations and methods
What does 'hello'.upper() return?
Which method removes whitespace from both ends?
What is 'Python'[::-1]?
Which method finds first occurrence of substring?
What does 'a,b,c'.split(',') return?
Which method replaces all occurrences?
What is ' hello '.strip()?
Which checks if string starts with prefix?
What does '-'.join(['a', 'b', 'c']) produce?
Which method returns string length?
Explain the difference between find() and index() methods.
[3 marks]What is string slicing? Explain start:stop:step with examples.
[3 marks]Differentiate between isalpha(), isdigit(), and isalnum().
[3 marks]Explain the join() method. Why is it preferred over string concatenation in loops?
[3 marks]What are f-strings? How do they improve string formatting?
[3 marks]Explain the difference between capitalize(), title(), and upper().
[3 marks]What is the zfill() method? When is it useful?
[3 marks]Explain partition() and rpartition() methods with examples.
[3 marks]What is string interning? Why does Python do it?
[3 marks]Explain maketrans() and translate() methods for character replacement.
[3 marks]Write a program to check if a string is a palindrome (ignoring case and spaces).
[5 marks]Create a program to count vowels, consonants, digits, and special characters.
[5 marks]Write a program to find the longest word in a sentence.
[5 marks]Create a Caesar cipher program to encrypt/decrypt text.
[5 marks]Write a program to format a number as currency (e.g., $1,234.56).
[5 marks]Create a password validator (min 8 chars, upper, lower, digit, special).
[5 marks]Predict output: s = 'hello' print(s[1:4]) print(s[:3]) print(s[3:])
[3 marks]Explanation:[1:4] is indices 1,2,3='ell'. [:3] is 0,1,2='hel'. [3:] is 3 onwards='lo'.
Find error: text = 'hello' text[0] = 'H' print(text)
[2 marks]Explanation:Strings are immutable. Use text = 'H' + text[1:] or text.replace('h', 'H').
Predict output: words = ['Python', 'is', 'fun'] print(' '.join(words)) print('-'.join(words))
[2 marks]Explanation:join() concatenates list elements with separator between them.
Find error: print('Value: %d' % '5')
[2 marks]Explanation:%d requires integer. Use %s for strings or convert: %d % int('5').
Predict output: s = 'hello world' print(s.find('o')) print(s.rfind('o')) print(s.find('z'))
[3 marks]Explanation:find() returns first 'o' at 4. rfind() returns last 'o' at 7. find('z') returns -1 (not found).
Find error: name = 'john' print(name.capitalize()) print(name)
[2 marks]Explanation:Strings are immutable. capitalize() returns NEW string. Original 'name' stays 'john'.
Predict output: s = 'abc123' print(s.isalnum()) print(s.isalpha()) print(s.isdigit())
[3 marks]Explanation:isalnum() true (letters+digits). isalpha() false (has digits). isdigit() false (has letters).
Find error: result = 'hello'.split('')
[3 marks]Explanation:Cannot use empty string as separator in split(). Use list('hello') to get individual characters.