Introduction to Python data type methods a ———— the use of strings

Source: Internet
Author: User


String is one of the important data types in Python, and the strings are not modifiable.

A string is a set of characters between quotation marks (single, double, and triple quotes ) . (The string must be within quotation marks and the quotation marks must be paired)

Note: single, double and triple quotation marks are not much different in use;

The quotation marks can be used in a cross-use way to avoid excessive escaping;

Single, double quotation marks implement line breaks must use a continuation character, and three quotation marks can be directly continued without the use of the continuation of the line symbol.

A. count, the number of occurrences of the statistics character or substring in a string

Format:s.count (sub[, start[, end]), int

Sub is the substring in the string to be queried start is the index position at which the start statistic returns a number at the end of the index position

>>> s= "Hello python"
>>> s.count (' l ')
>>> 2
>>> s
' Hello python '
>>> s.count (' O ')
2
>>> s.count (' o ', 4,6)
1

B. capitalize first letter capital

Format: s.capitalize () string

>>> s
' Hello python '
>>> s.capitalize ()
' Hello python '

C.isalnum,isalpha,isdigit,islower,isspace,istitle,isupper These are the character types in the judgment string, and the Boolean value is returned.

isdigit to determine whether a string contains only numbers

Format: S.isdigit () Boo

>>> s
' Hello python '
>>> S.isdigit ()
False
>>> s= ' Hello python 1 '
>>> S.isdigit ()
False
>>> s= ' 123 '
>>> S.isdigit ()
True

Islower determines whether the letters in a string are lowercase letters

Format: S.islower ()-bool

>>> s
' Hello python '
>>> S.islower ()
True
>>> s= "Hello python"
>>> S.islower ()
False

d. Lower/upoer/title The letters in the string into lowercase / uppercase / uppercase letters

Format: S.lower () string

S.upper (), string

S.title (), string

>>> s
' Hello python '
>>> S.lower ()
' Hello python '
>>> S.upper ()
' HELLO PYTHON '
>>> S.title ()
' Hello Python '

E. Startswith/endswith whether to start / end , return boolean value

Format: S.startswith (prefix[, start[, end]), BOOL

S.endswith (suffix[, start[, end]), BOOL

Prefix/suffix beginning/end Start index position end start index position returns a Boolean value

>>> s
' Hello python '
>>> s.startswith (' he ')
True
>>> s.startswith (' e ')
False
>>> s.startswith (' he ', 4,8)
False

F. replace string substitution

Format: S.replace (old, new[, Count]), string

Old replaced by the string new to replace the number of times a string is replaced by a string

>>> s
' Hello python '
>>> s.replace (' e ', ' e ')
' HEllo python '
>>> s.replace (' l ', ' l ',)
' HeLLo python '
>>> s.replace (' l ', ' l ', 1)
' HeLlo python '

G. Split/rsplit ( starting from the right ) string cutting

Format: S.split ([Sep [, Maxsplit]]), List of strings

S.rsplit ([Sep [, Maxsplit]]), List of strings

Sep Specifies the cut character maxsplit the maximum number of cuts returns a list of elements as strings

>>> s
' Hello python '
>>> s.split ("")
[' Hello ', ' python ']
>>> s.split ("L", 1)
[' He ', ' lo python ']
>>> s.rsplit ("L", 1)
[' Hel ', ' O python ']

H. jion converting an iterative data type to a string

Format: S.join (iterable), string

>>> s
' Hello python '
>>> s=s.split ("")
>>> S
[' Hello ', ' python ']
>>> "". Join (S)
' Hello python '

I. strip remove the end-to-end character specified by the string (a single character is a unit match)

Format: S.strip ([chars]), string or Unicode

>>> s
' Hello python '
>>> S.strip ("O")
' Hello python '
>>> S.strip ("n")
' Hello Pytho '
>>> s= ' Hello Olleh '
>>> S.strip ("he")
' Llo Oll '
>>> S.strip ("he")
' Llo Olleh '

Strip this usage is matched in characters, in the last example, to remove the He, the current face matches to L, not to the he end, then to the space, the space is not in the he, the end.

J. Find finds the position of the specified character, and returns 1 if none . Returns the index value if the first matching character is found

Format: S.find (sub [, Start [, end]]), int

The sub specifies the index position at which the character start starts, and end of the index position. Returns a number

>>> s= ' Hello python '
>>> s.find (' O ')
4
>>> s.find (' o ', 5,11)
10
>>> s.find (' a ')
-1

K. Index find the index position of the character, without reporting an exception

Format: S.index (sub [, Start [, end]]), int

>>> s
' Hello python '
>>> s.index (' h ')
0
>>> s.index (' H ', 5)
9
>>> S.index (' a ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Valueerror:substring not found

L. format string formatting

Format: S.format (*args, **kwargs), string

The args parameter, which specifies the location parameter, Kwargs keyword

>>> print "My name is {},my" {} ". Format (' Xiaoming ', 10)
My name is Xiaoming,my 10

The position of the parameter must be specified in the Python2.6

>>> print "My name is {0},my" {1} ". Format (' Xiaoming ', 10)
My name is Xiaoming,my 10

There is another way:

>>> print "My name is%s,my age is%d"% (' xiaoming ', 10)
My name is Xiaoming,my 10

%s Specifies that the data is a string and%d specifies a number

Both of these formatting strings are commonly used in Python to

String Connection:

There are three ways to concatenate strings, the addition of strings is the same as the Join method, but the principle is different because the string in Python is an immutable type, a new string is generated when you concatenate two strings with +, and a new string will need to be re-requested for memory. When the string of successive additions is many (a+b+c+d+e+f+ ...), inefficiency is inevitable. The Jion method uses a slightly more complex, but highly efficient connection to multiple characters, with only one memory request. In this method, Jion must be an iterative type, and of course, string literals can be concatenated using a string-formatted method.

string addition

>>> "Hello" + "python"
' Hellopython '

Join method for strings

>>> "". Join (["Hello", "python"])
' Hello Python

String formatting

>>> "%s%s%s"% (' hello ', ' ', ' python ')
' Hello python '

String multiplication

String multiplication is often used to print delimiters.

>>> print "=" *10
==========
>>> print "Python" *10


Introduction to Python data type methods a ———— the use of strings

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.