how to search for a capital letter within a string and return the list of words with and without capital letters

前端 未结 9 1187
无人共我
无人共我 2020-12-18 23:23

My homework assignment is to Write a program that reads a string from the user and creates a list of words from the input.Create two lists, one containing the words that con

相关标签:
9条回答
  • 2020-12-18 23:46

    My regex:

    vendor  = "MyNameIsJoe. IWorkInDDFinc."
    ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor)
    

    I need split word that would have happened: My Name Is Joe. I Work In DDF inc.

    0 讨论(0)
  • 2020-12-18 23:50

    Hint: "Create two lists"

    s= input("Enter your string: ")
    withcap = []
    without = []
    for word in s.strip().split():
        # your turn
    

    The way you are using the for .. else in is wrong - the else block is executed when there is no break from the loop. The logic you are trying to do looks like this

    for c in s:
        if c.isupper():
            # s contains a capital letter
            # <do something>
            break # one such letter is enough
    else: # we did't `break` out of the loop
        # therefore have no capital letter in s
        # <do something>
    

    which you can also write much shorter with any

    if any(c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for c in s):
         # <do something>
    else:
         # <do something>
    
    0 讨论(0)
  • 2020-12-18 23:50

    Thank you to everyone for your input and help, it was all very informative. Here is the answer that I finally submitted.

    s = input("Please enter a sentence: ")
    withcap = []
    without = []
    for word in s.split():
        if word.islower():
            without.append(word)
        else:
            withcap.append(word)
    
    onelist = withcap + without
    
    for word in onelist:
        print (word)
    
    0 讨论(0)
提交回复
热议问题