There is text in the variable a :

 a = '''sherlock holmes, study in scarlet. she baker street journal.how would you describe.''' 

Is it possible in Python 3 to translate the first character after a point to upper case?

2 answers 2

This is how the first character of each sentence can be translated into uppercase:

 a = '''sherlock holmes, study in scarlet. she baker street journal.how would you describe.''' # разбиваем весь текст на предложения sentences = a.split('.') final_sentences = [] # обрабатываем каждое предложение for sentence in sentences: # удаляем пробелы в конце и начале предложения sentence = sentence.strip() # если предложение пустое - переходим к следующему if not sentence: continue # переводим первый символ предложения в верх. регистр sentence = sentence.capitalize() final_sentences.append(sentence) # склеимаем предложения обратно final_text = '. '.join(final_sentences) # добавляем точку в конец final_text = final_text + '.' # Результат: # Sherlock holmes, study in scarlet. She baker street journal. How would you describe. print(final_text) 
  • one
    1- str.capitalize not only translates the first character in upper case, but also makes all other letters small (that is, it is somewhat more than the author asked, although it is a reasonable extension of the task and even better than the naive a[0].upper() + a[1:] (for example, which character to choose for a register can depend on the position in a word in Unicode and str.capitalize() can take this into account.) 2- in general, text.split('.') not required sentences to return. See: String manipulation: capitalize first letter of every sentence - jfs
  • 1) Did not know that capitalize rest translates into lower case, sensible remark. 2) Of course, this is a big simplification and in real life can only be an example. - Egor Smolyakov
  • Thanks everything turned out - leonidtime

with original spaces and other non-displayable characters

 def isspace(iter_text: iter) -> iter: '''выдать пробелы до первого значащего символа и сам символ в верхнем регистре''' for s in iter_text: if s.isspace(): yield s # неотображаемые символы(пробелы) else: yield s.upper() # к верхнему регистру break # до первого значащего символа def upper_text(text: str) -> iter: iter_text = iter(text) yield from isspace(iter_text) # первый значащий символ текста к верхнему регистру for s in iter_text: yield s if s == '.': # значащий символ после '.' к верхнему регистру yield from isspace(iter_text) text = ''' sherlock holmes, study in scarlet. she baker street journal.how would you describe.''' result = ''.join(upper_text(text)) 
  • Thanks, everything turned out - leonidtime