Python Note 5 (string)-20160921

Source: Internet
Author: User

A common method of string

1. Title (): Returns the title version of the string, that is, the first letter of the word and the remaining letter lowercase

2, Upper (): Returns all uppercase versions of the string, lower () returns all lowercase versions of the string

>>> s = "Hello, world"
>>> S.upper ()
' HELLO, World '

3, Swapcase (): Returns the version of the string case interchange

>>> s = "ABABCDEFABC"
>>> S.swapcase ()
' Ababcdefabc '

4, Isalnum (): Check whether all characters are alphanumeric

>>> s = "ABC 123"
>>> S.isalnum ()
False
>>> a = "ABC123ABC"
>>> A.isalnum ()
True

5, Isalpha () Check if there are only letters in the string; IsDigit () checks if all characters are numeric, islower () checks if all characters are lowercase, istitle () checks whether the string is a heading style; Isupper () Checks whether the string is uppercase for all characters

6. Split (): Splits any string, split () allows a parameter to be used to define what character string to split, default to ""

>>> s = "What:is:your:name"
>>> s.split (":")
[' What ', ' is ', ' your ', ' name ']

7. Join () linked multiple strings using the specified word, which requires a list of string elements as input and then links the string elements in the list

>>> "-". Join ("So, how can I do for you?"). Split ())
' So,-what-can-i-do-for-you? '

Second, the string stripping

1, strip (chars) is used to peel the characters specified in the end of the string, without specifying the parameters will be stripped off the end of the default space and line breaks

>>> a = "a bc\n"
>>> A.strip ()
' A BC '

2. Use Lstrip (chars) or Rstrip (chars) to split the string left or right

>>> s = "www.baidu.com"

>>> s.lstrip ("www.")
' Baidu.com '

>>> s.rstrip ('. com ')
' Www.baidu '

Third, Text Search

1. Find () finds the first matching string, returns 1 if not found

>>> s = "Hello, world"
>>> s.find ("Hello")
0
>>> S.find ("abc")
-1

2, Starswitch (chars) checks whether the string starts with the specified character, Endswitch () checks whether the string ends with the specified string

Python Note 5 (string)-20160921

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.