What does the len() function do when applied to a string in Python?
a) Converts the string to uppercase
b) Returns the length of the string
c) Reverses the string
d) Removes whitespace from the string
Answer:
b) Returns the length of the string
Explanation:
The len()
function in Python returns the length of a string, which is the number of characters it contains, including spaces and special characters.
message = "Hello"
length = len(message) # length is 5
Using the len()
function is a simple way to determine the size of a string, which can be useful for tasks such as validation, formatting, and text processing.
Understanding the len()
function is essential when working with strings, as it helps you manage and manipulate text data effectively.