Python Viva Questions
Python Viva Questions
print(display("Divya"))
8. What is a string in Python?
A string is a sequence of characters enclosed within single quotes (') or double quotes
("). In Python, strings are immutable, meaning their contents cannot be modified after
creation.
9. How do you check the length of a string in Python?
You can use the built-in len() function to check the length of a string, which
returns the number of characters in the string.
10. What are some common string methods in Python?
upper(): Converts all characters to uppercase.
lower(): Converts all characters to lowercase.
capitalize(): Capitalizes the first character of the string.
strip(): Removes leading and trailing spaces.
replace(old, new): Replaces all occurrences of a substring with a new substring.
split(): Splits a string into a list of substrings.
join(): Joins a list of strings into a single string.
find(sub): Returns the index of the first occurrence of a substring, or -1 if not
found.
count(sub): Returns the number of occurrences of a substring.
11. What is string slicing in Python?
String slicing allows you to extract a portion of a string using the syntax
string[start:end:step], where:
start: The starting index (inclusive).
end: The ending index (exclusive).
step: The step between indices (optional).
12. What is the difference between + and * operators when used with strings?
+ is used for concatenation of two strings.
* is used for repeating a string a given number of times.
13. What are escape sequences in Python strings?
\n - Newline
\t - Tab
\\ - Backslash
\' - Single quote
\" - Double quote
14. How do you check if a string contains only digits in Python?
You can use the isdigit() method, which returns True if all characters in the string are
digits, and False otherwise.
15. How do you convert a string to an integer in Python?
You can use the int() function to convert a string that represents a number into an
integer.
16. What are loops in Python?
Loops in Python allow you to execute a block of code repeatedly, either for a fixed
number of times or until a condition is met. Python has two primary types of loops:
for loop and while loop.
for item in iterable:
# code block
while condition:
# code block
17. How does the range() function work in a for loop?
The range() function generates a sequence of numbers, which is often used in a for
loop for iteration. The syntax of range() is:
range(start, stop, step)
start: The starting value (default is 0).
stop: The end value (exclusive).
step: The increment value (default is 1).
18. What is a break statement in Python?
The break statement is used to exit from a loop prematurely, regardless of whether the
loop condition is met or not. It is typically used when a specific condition is met
inside the loop
19. How can you iterate over a list or string using a for loop?
my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item) # Output: 1, 2, 3, 4, 5
20. How Negative Indexing Works
my_list = [10, 20, 30, 40, 50]