Loading samples...
Loading samples...
py-sample-4
Mastering User Interaction and String Manipulation
What does input() function return?
Which function displays output on screen?
What is the default end parameter of print()?
How to get length of a string?
What is 'Hello' + 'World'?
What does 'Hi' * 3 produce?
Which escape sequence creates a new line?
What is s[0] if s = 'Python'?
What does s[-1] return?
How to format: print('Value:', x)?
Explain the difference between input() and print() functions.
[3 marks]What is string concatenation? Give two ways to concatenate strings.
[3 marks]Explain string indexing with positive and negative indices.
[3 marks]What are f-strings? How are they different from regular strings?
[3 marks]Explain the sep and end parameters of print() function.
[3 marks]What is string immutability? Why are strings immutable in Python?
[3 marks]Explain string slicing syntax with start, stop, and step.
[3 marks]How to convert a number to string and vice versa?
[2 marks]What is the difference between str() and repr()?
[3 marks]Explain raw strings with an example. When are they useful?
[3 marks]Write a program to get user's name and print a personalized greeting.
[5 marks]Create a program that takes two numbers and prints their sum, difference, product, and quotient.
[5 marks]Write a program to format and display a student's marks card with name, marks, and percentage.
[5 marks]Create a program that reverses a user-input string using slicing.
[5 marks]Write a program to demonstrate all string repetition and concatenation operations.
[5 marks]Create a program that extracts first, middle, and last characters from a string.
[5 marks]Find error: age = input('Age: ') print('In 5 years:', age + 5)
[3 marks]Explanation:input() returns string. Convert: int(age) + 5 or age = int(input(...))
Predict output: s = 'Python' print(s[0]) print(s[-1]) print(s[2:4])
[3 marks]Explanation:[0] is first char 'P', [-1] is last 'n', [2:4] is index 2,3 = 'th'
Find error: s = 'Hello' s[0] = 'h' print(s)
[3 marks]Explanation:Strings are immutable. Use s = 'h' + s[1:] or s.replace('H', 'h').
Predict output: print('A', 'B', 'C', sep='-', end='!')
[2 marks]Explanation:sep='-' joins arguments with -, end='!' replaces default newline with !
Find error: print('Result: %d' % '5')
[2 marks]Explanation:%d expects integer. Use %s for string or convert to int first.
Predict output: s = 'abcd' print(s[::2]) print(s[::-1])
[2 marks]Explanation:[::2] takes every 2nd char starting from 0. [::-1] reverses the string.
Find error: name = input('Name: ') print(f'Hello, {Name}')
[2 marks]Explanation:Case-sensitive! Variable is 'name' (lowercase), used 'Name' (capitalized).
Predict output: a = '5' b = '10' print(a + b) print(int(a) + int(b))
[3 marks]Explanation:First: string concatenation '5'+'10'='510'. Second: integer addition 5+10=15.