In: Computer Science
python:
Write a program that turns a sentence into camel case. The first word is lowercase, the rest of the words have their initial letter capitalized, and all of the words are joined together. For example, with the input "fOnt proCESSOR and ParsER", your program will output "fontProcessorAndParser".
Optional: can you do this with a list comprehension?
Optional extra question: print a warning message if the input will not produce a valid variable name. You don't need to be exhaustive in checking, but test for a few common issues, such as starting with a number, or containing invalid characters such as # or + or ".
Test your program, and comment your code.
Python Code Screenshot with Output :
Python Code :
s = input("Enter a sentence :
") #
Taking input of sentence
str_arr =
s.split()
# splitting the input sentence into words
result =
""
# initializing result as empty string
result +=
str_arr[0].lower()
# Converting first word ( word at index 0 ) of the input sentence
into its lowercase
for i in
str_arr[1:]:
# Now, we'll start iterating from index 1 and onwards
result +=
(i.capitalize())
# Capitalizing rest of the words
print("Output : " ,
result)
# printing the required resultant string
Note : Hi, whenever we paste the code in the answer box, the indentation of the code gets disturbed. And as we know that It is necessary to indent the code in python before running, to avoid any indentation error. So, Before running the code, Please indent the provided code ( as in the above screenshot ). Otherwise, you might get indentation error.