Computer Science
What would the following expressions return?
- "Hello World".upper( ).lower( )
- "Hello World".lower( ).upper( )
- "Hello World".find("Wor", 1, 6)
- "Hello World".find("Wor")
- "Hello World".find("wor")
- "Hello World".isalpha( )
- "Hello World".isalnum( )
- "1234".isdigit( )
- "123FGH".isdigit( )
Answer
- hello world
- HELLO WORLD
- -1
- 6
- -1
- False
- False
- True
- False
Explanation
- upper() first converts all letters of "Hello World" to uppercase. Then "HELLO WORLD".lower() converts all letters to lowercase.
- lower() first converts all letters of "Hello World" to lowercase. Then "hello world".upper() converts all letters to uppercase.
- "Hello World".find("Wor", 1, 6) searches for the presence of substring "Wor" between 1 and 6 indexes of string "Hello World". Substring from 1 to 6 index is "ello W". As "Wor" is not present in this hence the result is False.
- "Hello World".find("Wor") searches for the presence of substring "Wor" in the entire "Hello World" string. Substring "Wor" starts at index 6 of "Hello World" hence the result is 6.
- "Hello World".find("wor") searches for the presence of substring "wor" in the entire "Hello World" string. find() performs case sensitive search so "wor" and "Wor" are different hence the result is -1.
- "Hello World".isalpha( ) checks if all characters in the string as alphabets. As a space is also present in the string hence it returns False.
- "Hello World".isalnum( ) checks if all characters in the string are either alphabets or digits. As a space is also present in the string which is neither an alphabet nor a string hence it returns False.
- "1234".isdigit( ) checks if all characters in the string are digits or not. As all characters are digits hence the result is True.
- As "FGH" in the string "123FGH" are not digits hence the result is False.
Related Questions
From the string S = "CARPE DIEM", which ranges return "DIE" and "CAR"?
What happens when from a string slice you skip the start and/or end values of the slice?
Which functions would you choose to use to remove leading and trailing white spaces from a given string?
Try to find out if for any case, the string functions isalnum( ) and isalpha( ) return the same result