2017-10-04 4 views
-2

文全体を大文字にする例外はありますか?私はskipListメソッドについて聞いたことがありますが、私のコードではうまくいきませんでした。下記を参照:文字列を大文字にするための例外を作成するさまざまなメソッド

string = input('Enter a string: ') 

i = 0 
tempString = ' '.join(s[0].upper() + s[1:] for s in string.split(' ')) 
result = "" 

for word in tempString.split(): 

    if i == 0: 
     result = result + word + " " 
    elif (len(word) <= 2): 
     result = result + word.lower() + " " 
    elif (word == "And" or word == "The" or word == "Not"): 
     result = result + word.lower() + " " 
    else: 
     result = result + word + " " 
    i = i + 1 

print ("\n") 

print (result) 
+2

コピーのように、これを書き換え、あなたのコードの代わりに、画像をフォーマットすることができます。 –

+0

フォーマットを編集しました – Anonymous

+1

N.B.あなたの 'tempString'行は単に' tempString = string.title() 'です。 –

答えて

0

確かに。 タイトルの大文字と小文字を区別しない単語のリスト( "and"、 "or"、 "not"など)とtitle-caseのその他すべてのものを書きます。当然の

words = s.split(' ') 

result = ' '.join([words[0]] + [w.title() for w in words[1:] if w not in skipwords]) 

これはまだ大文字すべき、ミスターないの姓を欠場すること、および"McFinnigan"のようないくつかの見知らぬ人の事は間違っているだろうが、言語は難しいです。それ以上のことを望むなら、おそらくNTLKを調べなければならないでしょう。

0

あなたはこの

skip_words = {w.capitalize(): w for w in 'a in of or to and for the'.split()} 
words = string.title().split() 
result = ' '.join(skip_words.get(w, w) for w in words).capitalize() 
+0

私はこのメソッドを実行しました。 2つの文字の単語を言及する必要はありませんし、自動的にそれらを大文字にしませんskip_wordsにいない、なぜですか? – Anonymous

+0

例を挙げてもらえますか? 'str.title()'は、長さにかかわらず、すべての単語を大文字にしていることは確かです。 –

関連する問題