Python (String manipulation Example 1) A string separated by a space

Source: Internet
Author: User
# separate words in words with spaces
# only letters are known in the passed-in string, with the first letter of each word capitalized,
# Separate each word with a space, leaving only the first letter capitalized in: "HelloMyWorld"
# return to "Hello My World"


# given a string
INSTR = "HelloMyWorld"
# Convert strings to lists
Str_list = List (INSTR)

# take out each element with a loop
For I in INSTR:
# Determine if an element is uppercase
If I.isupper ():
# if it's uppercase, record the subscript position
index = Str_list.index (i)
# Judging if it's the first letter, jump out of this loop
If index = = 0:
Continue
# Modify data, convert uppercase to Cheng and add spaces
Str_list[index] = I.lower ()
Str_list.insert (Index, "")

# Convert to String
outstr = "". Join (Str_list)
Print (OUTSTR)

Python (String manipulation Example 1) A string separated by a space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.