Write a python code for the following function:
Take input from the user and capitalize the second letter of each word and print it.
Example cases:
Input -> hi there
Output -> hI tHere
Input -> python is extrememly good
Output -> pYthon iS eXtremely gOod
No spam! :)
Answers
Capitalizing Second Letters - Python
I will be putting here two programs. Both have their own assumptions of the input and output criteria.
The General Logic is to first take in the user input of a string, convert it into a list of characters, and run through it to capitalize the second letter.
The methods used:
- len(string)
It returns the length of the string passed as parameter
- str.isalpha()
It does not take any parameters. It returns True if all characters in the string str are alphabets, and False if even one of them is not.
We will be checking individual characters with this method.
- str.upper()
This method capitalizes the string str passed to it and returns it.
- str.join(iterable)
This method joins the elements of the iterable (we will be using a list here) with the str string in between.
So, if we had a list
my_list = ['a', 'b', 'c']
And if we want to join them with, say # in between, we would do:
'#'.join(my_list)
This would return "a#b#c"
We can use an empty string so as to join all elements directly without anything in between.
Let's get to the programs.
The First Program - StringFun.py
This program works correctly if no special characters are passed to it, just like in the sample input shown in the question. If any special characters are to be passed, it would misinterpret the second character.
So you have to be pretty restricted in the input.
Here goes the code.
# Python Program to take input from user and
# capitalize second letter of each word
sentence = input("Enter your string: ") # Taking User Input
# Make a list of characters in the sentence string
characters = [char for char in sentence]
length = len(characters) # Storing length in a variable
# If the first character is an alphabet, check if second character exists
if characters[0].isalpha():
if length>1: # Condition to check if second character exists
if characters[1].isalpha():
# If second character is an alphabet as well, capitalize it
characters[1] = characters[1].upper()
# Run a loop to check for groups of <space><alphabet><alphabet>
# and capitalize the second letter
for i in range(length-2):
if characters[i] == ' ' and characters[i+1].isalpha() and characters[i+2].isalpha():
characters[i+2] = characters[i+2].upper()
# Join all the characters
# Nothing is to be inserted between them, hence the empty string ''
newsentence = ''.join(characters)
print(newsentence) # Print out the new sentence
Screenshots of the code and terminal runs are attached. They are the first two of the four screenshots.
The Second Program - AdvancedStringFun.py
This program assumes the following:
- Space(s) mark(s) a new word
- "Letters" mean Alphabets, so second alphabet should be capitalized
- There may exist any non-alphabets between the first and second one
- If a word has only one alphabet, it cannot be capitalized, regardless of any numbers or other characters (except space) preceding it
This program works pretty well for any case. It covers cases where the first program fails.
Here goes the code.
# Python Program to take input from user and
# capitalize second letter of each word
sentence = input("Enter your string: ") # Taking User Input
# Make a list of characters in the sentence string
characters = [char for char in sentence]
length = len(characters) # Storing length in a variable
# We will use two boolean flags
newWord = True
firstLetterReached = False
i = 0
while i < length: # Outer Loop to run through all characters
# If newWord is True, search for first letter
# When first letter is reached, search for second letter and capitalize it
if newWord:
if(firstLetterReached): # Condition when first letter of word has been found
while i < length:
# If a space is found, firstLetter was the only letter in the word
# Hence, set firstLetterReached to False and break loop
if characters[i] == ' ':
firstLetterReached = False
i += 1
break
if characters[i].isalpha():
# When second letter is found, capitalize it
characters[i] = characters[i].upper()
i+=1
firstLetterReached = False
newWord = False
break
i += 1
else: # Condition when first letter of word is yet to be found
while i < length:
if characters[i].isalpha():
firstLetterReached = True
i += 1
break
i += 1
else: # Condition when newWord is False
while i < length:
# Search for a space
if characters[i] == ' ':
newWord = True
i += 1
break
i += 1
# Join all the characters with empty string '' in between them
newsentence = ''.join(characters)
# Print out the new sentence
print(newsentence)
The last two screenshots of the four are of this second program.
Concepts involved :
Basic application of string manipulation
1.string slicing
2.string traversing
3.string addition :adding two strings
4.string capitalize function: capitalizes the chosen word or string
5.split function : to break a sentence into individual words that are stored in a list.
6.string indexing
(I also tried to keep in mind space complexity and time complexity of the program)
#Program to capitalize 2nd letter of each word
str=input('enter the string:')
a=str.split()
l=len(a)
c=''
for i in range(0,l):
if len(a[i])>1:
#string indexing
b=a[i][0:1]+a[i][1].capitalize()+a[i][2:]
c=c+b+' '
else:
c=c+a[i]+' '
print(c)
Hope this helps