0
1)Prompt the user for a string that contains two strings separated by a comma.
2)Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings.
3)Using string splitting, extract the two words from the input string and then remove any spaces. Output the two words.
4)Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit.
出力された単語に付加されている余分なスペースを削除する方法はありません。たとえば、「Billy、Bob」と入力すると正常に動作しますが、「Billy、Bob」と入力すると、IndexError:listインデックスが範囲外になります。または、「Billy、Bob」と入力すると、文字列に余分なスペースが付いています。ここに私のコードです。Pythonで文字列を分割した後に空白を削除する
usrIn=0
while usrIn!='q':
usrIn = input("Enter input string: \n")
if "," in usrIn:
tokens = usrIn.split(", ")
print("First word:",tokens[0])
print("Second word:",tokens[1])
print('')
print('')
else:
print("Error: No comma in string.")
出力からスペースを削除して、usrIn.split( "、")を使用することはできますか?