-2

In python, I want a program that turn the first leter on a word capital letter.

For exemple:

turn "a red apple is sweeter than a green apple" in "A Red Apple is Sweeter Than A Green Apple"

How can I do?

I've tried this:

d = input('insert a quote')
def mydic(d):
  dic = {}
  for i in d:
    palavras = dic.keys()
    if i in palavras:
      dic[i] += 1
    else :
      dic[i] = 1
return dic
ACVM
  • 1,497
  • 8
  • 14

2 Answers2

1

You could use the title() method.

For example:

sentence = str(input("Insert a quote: ")).title()
print(sentence)

Input: a red apple is sweeter than a green apple

Output: A Red Apple Is Sweeter Than A Green Apple

0

What you want to do is this:

  1. split the input string into words ie. string.split(' ') splits a given string by spaces, returns a list.
  2. for each word, capitalize the first letter and concatenate onto a bigger string ie. word[:1].upper() + word[1:] this will uppercase the first letter
  3. Add all the words back into a list and return it.
ACVM
  • 1,497
  • 8
  • 14