HackerRankInterview QuestionsPythonPython InterviewRandom

HackerRank Capitalize! problem solution in Python | python problem solution

You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, alison heck should be capitalised correctly as Alison Heck.

Given a full name, your task is to capitalize the name appropriately.

Input Format

A single line of input containing the full name, .

Constraints

  • The string consists of alphanumeric characters and spaces.

Note: in a word only the first character is capitalized. Example 12abc when capitalized remains 12abc.

Output Format

Print the capitalized string, .

Sample Input

chris alan

Sample Output

Chris Alan

Problem solution in Python 2 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import string

words = raw_input().split(' ')
for i in xrange(len(words)):
words[i] = string.capitalize(words[i])

print ' '.join(words)

Problem solution in Python 3 programming.


# Complete the solve function below.
def solve(s):
for x in s[:].split():
s = s.replace(x, x.capitalize())
return s

Problem solution in pypy programming.

def capitalize(string):
os=""
for idx,x in enumerate(string):
#print (x,idx,str1[idx-1])
os+=x.upper() if string[idx-1]==" " or idx == 0 else x
return "".join(os)

Problem solution in pypy3 programming.

def capitalize(string):
s=""
space=True
for x in string :
if space and x!=' ':
s=s+x.upper()
space=False
elif x==' ':
s=s+x
space=True
else :
s=s+x

return s


Leave a ReplyCancel reply

Exit mobile version