One, the standard library string for Python

Source: Internet
Author: User

One, the standard library string for Python

1. View weapons

A

Help (Type ())

name = "Jane"
Print (Type (name))

B

Capitalize ()


Print (Name.capitalize ())

Effect: Jane

C

Center ()

Print (Name.center (50, '-'))

effect :-----------------------Jane-----------------------

D

Count ()

message = "Don ' t touble me"
Print (Message.count ("T"))
Print (Message.count ("T", 0, 7))

Effect: View the number of occurrences of the specified index range, or do not specify a range (do not specify a range from left to right)

E

EndsWith ()

message = "Don ' t touble me"
Print (Message.endswith ("Me"))
Print (Message.endswith ("le", 6, 12))
Print (Message.endswith ("T", 1, 5))

Effect: see if the specified element is the end, true, no false

F

StartsWith ()

Effect: see if the specified element is the start, no false, is true

G

Expandtabs ()

s = "Jane\tjane"
Print (S.expandtabs ())
Print (S.expandtabs (25))

Effect: Converts tab to space, default one tab to 8 spaces

H

Find ()

message = "Hey what's Up, It's been while."
Language = "中文版 Chinese Python c C"
Print (Message.find ("E"))
Print (Message.find ("E", 2, 30))
Print (Message.find ("E", 23, 30))
Print (Language.find ("sh"))
Print (Language.find ("Li", 2, 6))

Effect: View the index value of an element, or view the index value of an element of a specified index range

I

Index ()

Effect: Same as Find (), find () not found will return -1,index () will error

J

Isalnum ()

Effect: To see if a string contains letters and numbers, is true, no false

K

Isalpha ()

Effect: See if it is a letter

L

IsDigit ()

Effect: See if it is a number

M

Islower ()

Istitle ()

Isupper ()

N

Isspace ()

Effect: See if a string consists of spaces

O

Join ()

usr = "Gjlove"
names = [' Jane ', ' Guo ', ' hate ']
Print (Usr.join (names))

Effect: Janegjloveguogjlovehate, Ganso can also

P

Rjust ()

Ljust ()

Effect: ________________jane

Q

Lstrip ()

Rstrip ()

Strip ()

Effect: Remove whitespace, default is a space, or it can be a number

R

Partition ()

Message = "How does you do?"
Print (Message.partition ("Do")

Effect: (' How ', ' does ', ' do ')

S

Replace ()

s = "Hello Jane Hello everybody Hello Nothing"
Print (S.replace (' Hello ', ' Hi ', 3))

Effect: Hi Jane hi everybody hi Nothing

T

Frind ()

message = "Hello Python, hello Java, hello C," Hello C + + "
Name = "Janejanejane"
Print (Message.rfind ("E"))
Print (Name.rfind ("E"))
Print (Name.rfind ("E", 0, 8)) #从右往左查询最后一次出现的 "E"
Print (Name.rfind ("G")) #没有会返回-1

Effect:

36
11
7
-1

U

Rindex ()

V

Rpartition ()

name = "Jane and Jane"
Print (Name.rpartition (' J ')) #从右向左找分成三部分

Effect: (' Jane and ', ' j ', ' ane ')

W

Split ()

Rsplit ()

name = "Jane Lala Rose and Jack"
Print (Name.rsplit ("and")) #从右向左以 "and" cut, Effect [' Jane Lala Rose ', ' Jack ']
usr = "Janejanejane"
Print (Usr.rsplit ("a", 1)) #从右向左以 "A" cut, the rest "a" does not cut.
#split是从左往右切割

Effect:

[' Jane Lala Rose ', ' Jack ']
[' janejanej ', ' ne ']

X

Splitlines ()

Name = "Jane\njane"
Print (Name.splitlines (True)) #以换行符为分割, the argument is True when the newline character \ n is preserved.

Effect: [' jane\n ', ' Jane ']

Y

Swapcase ()

Title ()

Name = "Janee"
Print (Name.swapcase ()) #小写变大写, uppercase to lowercase.
Print (Name.title ())

Effect:

Janee
Janee

Z

Zfill ()

message = "Hey what's up, it's been a while."
Print (len (message))
Print (Message.zfill (35))
Print (Message.zfill) #字符串message长度是33, more than a few to complement how many zeros.

Effect:

00hey What's up, it's been a while.
00000hey What's up, it's been a while.

One, the standard library string for Python

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.