Python case lettering and spacing questions.

Asked 2 years ago, Updated 2 years ago, 73 views

I'm asking you a question because I thought of it while I was learning Python by myself.
When you enter an English name without spacing as follows RyuHyunJin

as follows [Ryu, Hyun, Jin] is about to be output.
ifWhen lowercase letters come in, you can attach them to the left letter and capital letters to the left, but it's hard to find an example of a function related to upper and lowercase letters. Please help me

python split

2022-09-20 20:12

2 Answers

A frustrating answer: If you use a regular expression, it's a story that ends in Hankyu.

import re

# Matches when one or more non-capitalized characters appear once capitalized
regex = r"[A-Z][^A-Z]+"

test_strs = ("RyuHyunJin", "Gwang-hyunKim")

for s in test_strs :
    print(' '.join(re.findall(regex, s)))

A more sincere answer:

Of course, it's also important to branch out to if to determine whether "this letter is capitalized or not." (Note, there's a function called ord().)
But more importantly, the key idea is that you have to go around a given string by letter.

This is what happens in Python.

for letter in "RyuHyunJin" :
    print(letter) # Imagine what will be printed and then run it.

Try it yourself from here.


2022-09-20 20:12

If you make it very, very easy...

Case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Input = "HyunJinRyu"

Output = ""

Enter for letter in:
    If letter in uppercase:
        Output +=""
    Output + = characters    

print (output)

Apply it and make it better.


2022-09-20 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.