No results found for the specified position. Return the string in lowercase. Python Beginner Level Strings Mock Interview

MockQuestions

Python Beginner Level Strings Mock Interview

Question 2 of 10 for our Python Beginner Level Strings Mock Interview

Get More Information About Our Python Beginner Level Strings Interview Questions

Question 2 of 10

Return the string in lowercase.

This interview question tests the developer's skills with Python strings and loops.

You are given a string. The string contains the English alphabet. You need to implement a function toLowerCase which returns the same string in lowercase letters. You can not use the inbuilt function str.lower().

/*Example*/

given string- 'abcAGiHkj'
expected output- 'abcagihkj'

given string- 'qwert'
expected output- 'qwert'

Solution:

We will iterate over each character of the given string and convert it into lowercase. Then we will push the resulting character into a result array. After iterating the string, we will join the result array and return it. Now the problem remaining is to convert a character to lowercase.

The lowercase letters have character codes from 97 to 122 (both inclusive) and uppercase have from 65 to 90 (both inclusive). We will check the code of the letter. If it is between 97 and 122, we need not change the character. If it is between 65 and 90, we will add 32 (= 97 - 65) to convert to lowercase. So if-
the letter is 'j'
-> its code is 106
-> between 97 and 122
-> already a lowercase letter
the letter is 'B'
-> its code is 66
-> between 65 and 90
-> add 31
-> 66 + 32 = 98
-> 98 is 'b'

def toLowerCase(string: str) -> str:
	result = []
	A_ascii = ord('A')	# 65
	Z_ascii = ord('Z')	# 90

	for char in string:
		ascii_code = ord(char)

		if ascii_code >= A_ascii and ascii_code <= Z_ascii:
			# change ascii to make capital to lower
			char = chr(ascii_code + 32)
		result.append(char)

	return ''.join(result)

Time complexity- O(n)
Space complexity- O(n)

Written by on June 27th, 2021

Next Question

How to Answer: Return the string in lowercase.

Advice and answer examples written specifically for a Python Beginner Level Strings job interview.

  • 2. Return the string in lowercase.

      This interview question tests the developer's skills with Python strings and loops.

      You are given a string. The string contains the English alphabet. You need to implement a function toLowerCase which returns the same string in lowercase letters. You can not use the inbuilt function str.lower().

      /*Example*/
      
      given string- 'abcAGiHkj'
      expected output- 'abcagihkj'
      
      given string- 'qwert'
      expected output- 'qwert'

      Solution:

      We will iterate over each character of the given string and convert it into lowercase. Then we will push the resulting character into a result array. After iterating the string, we will join the result array and return it. Now the problem remaining is to convert a character to lowercase.

      The lowercase letters have character codes from 97 to 122 (both inclusive) and uppercase have from 65 to 90 (both inclusive). We will check the code of the letter. If it is between 97 and 122, we need not change the character. If it is between 65 and 90, we will add 32 (= 97 - 65) to convert to lowercase. So if-
      the letter is 'j'
      -> its code is 106
      -> between 97 and 122
      -> already a lowercase letter
      the letter is 'B'
      -> its code is 66
      -> between 65 and 90
      -> add 31
      -> 66 + 32 = 98
      -> 98 is 'b'

      def toLowerCase(string: str) -> str:
      	result = []
      	A_ascii = ord('A')	# 65
      	Z_ascii = ord('Z')	# 90
      
      	for char in string:
      		ascii_code = ord(char)
      
      		if ascii_code >= A_ascii and ascii_code <= Z_ascii:
      			# change ascii to make capital to lower
      			char = chr(ascii_code + 32)
      		result.append(char)
      
      	return ''.join(result)

      Time complexity- O(n)
      Space complexity- O(n)

      Written by S. Kumar on June 27th, 2021