Python __python string slicing

Source: Internet
Author: User
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 ') 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.