简体   繁体   中英

making first letter of input uppercase with split in python

I got most of the code down but I am having issues getting the string to space back out after making the first letter of each word uppercase

here's what I have so far:

message = input('Write a short message.')

new_message = message.split()

glue = ""

for item in new_message:

glue += item[0].upper() + item[1:]

print(glue)

try with:

message.capitalize()

If you want to capitalize each word you can try capitalize() and the code will look like this:

  message = input('Write a short message.')
    
  new_message = message.split()
  cap_message = [x.capitalize() for x in new_message]
  print(cap_message)
  • message.split() - split the string into a list using default separator which is any whitespace. The result is a list of words.
  • capitalize each word in the list using List Comprehention . The list of capitalized words is saved in cap_message variable for code clarity.
  • print the list of capitalized words

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM