KnowledgeBoat Logo

Computer Science

What would the following expressions return?

  1. "Hello World".upper( ).lower( )
  2. "Hello World".lower( ).upper( )
  3. "Hello World".find("Wor", 1, 6)
  4. "Hello World".find("Wor")
  5. "Hello World".find("wor")
  6. "Hello World".isalpha( )
  7. "Hello World".isalnum( )
  8. "1234".isdigit( )
  9. "123FGH".isdigit( )

Python String Manipulation

21 Likes

Answer

  1. hello world
  2. HELLO WORLD
  3. -1
  4. 6
  5. -1
  6. False
  7. False
  8. True
  9. False

Explanation

  1. upper() first converts all letters of "Hello World" to uppercase. Then "HELLO WORLD".lower() converts all letters to lowercase.
  2. lower() first converts all letters of "Hello World" to lowercase. Then "hello world".upper() converts all letters to uppercase.
  3. "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.
  4. "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.
  5. "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.
  6. "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.
  7. "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.
  8. "1234".isdigit( ) checks if all characters in the string are digits or not. As all characters are digits hence the result is True.
  9. As "FGH" in the string "123FGH" are not digits hence the result is False.

Answered By

4 Likes


Related Questions