A string consisting of words separated by spaces is given. Form a new line in which all words are in lower case, except for the first letter of the first word.

Tried to replace via re.sub , this is the only thing that came to mind. I have to look for all the characters in the string and replace them with the same, but in lower case, sub seems to do this, but I don’t really understand what parameters to pass to it.

  • Shashechki you or go? Why regular expressions? - Nick Volynkin
  • The job is. It is necessary through the regulars. And I do not understand them at all. - Mr. Alex C
  • OK got it. But if the task for you to do, you will not begin to understand them. Tell at least what they were trying to do, with what difficulties? - Nick Volynkin
  • you have confused your clarification. what you need? replace the first character of a string? or "search for all characters in the string and replace them with the same"? - while1pass
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Solution 1 - .capitalize () method

Use the .capitalize () method, regular expressions are not needed here.

 >>> string = 'my favourite cool string' >>> string.capitalize() 'My favourite cool string'' 

Solution 2 - through regular expressions

This method will not be used by anyone in their right mind, many unnecessary actions only if, as a study of the possibilities of language and regular expressions

A few steps:

  1. You need to use the python re module's re.sub method to replace part of the string.
  2. We will replace the first character of the string that corresponds to the regular expression '^ \ w' - one character (digit or letter) at the beginning of the string.
  3. The replacement will be made with the same uppercase character (using the .upper () method for the first character of string [: 1].
 >>> string = 'my favourite cool string' >>> import re >>> new_string = re.sub(r'^\w', string[:1].upper(), string) >>> new_string 'My favourite cool string' 

Useful links for understanding regular expressions: one , two , three

And remember - "if a developer has decided to solve one problem through regular expressions, congratulations, he now has 2 problems."

  • through string methods will not work, you need it through regular expressions - Mr. Alex C
  • @ Mr.AlexC expanded the answer - while1pass
  • You need to s.casefold() to regex input (input is not required to be in lower case). If string methods cannot be used, then indexing and .upper() also cannot be used. If the alphabet is not limited, the task becomes much more complicated and reduces to the implementation of capitalize() only with the help of regular expressions (without looking at the Unicode standard, I can’t even say if this is possible — although it is probably enough to replace the first letter using one big table and the rest replace the letters using another table: the implementation of Upper and casefold, respectively). Although ascii is enough for studying - jfs
  • @jfs yes, there’s no problem at all. most likely some kind of artificial learning task, where it is necessary to apply regulars in some form. and thank you for enlightenment, - while1pass
  • I looked at the Unicode standard: two simple UnicodeData.txt and SpecialCasing.txt files are UnicodeData.txt , although some of the rules are interesting: 'Σa'.lower() == 'σa' , but 'aΣ'.lower() == 'aς' (if Σ at the end of the word, then a different result) and of course (known) "ß".upper() == 'SS' . pycasefold module that implements unicode.casefold for Python 2 , reading CaseFolding.txt shows how easy it is to recognize such files. s = re.sub(r'^(\w)(.*)', capitalize, s) - jfs