to slice a string
String ' xxx ' and unicode string u ' xxx ' can also be viewed as a list, each element being a character. Therefore, strings can also be sliced, but the result of the operation is still a string:
>>> ' ABCDEFG ' [: 3]
' ABC '
>>> ' ABCDEFG ' [-3:]
' EFG '
>>> ' ABCDEFG ' [:: 2]
' Aceg '
In many programming languages, many kinds of intercepting functions are provided for strings, but the goal is to slice the string. Python does not have an intercept function for a string, it is simple to slice one operation. Task
The string has a method upper () can turn a character into a capital letter:
>>> ' abc '. UPPER ()
' abc '
But it will turn all letters into uppercase. Design a function that takes a string and then returns a string with only the first letter turned into uppercase.
tip: use slicing to simplify string manipulation. What if it doesn't?
Take a string other than the first letter with [1:]
Reference Code:
def Firstcharupper (s): Return S[0].upper () + s[1:] Print firstcharupper (' hello ') print firstcharupper (' Sunday ') print Firstcharupper (' September ')