Python's core--string
1. What is a string
In fact, when it comes to data types, it is the argument with quotation marks, "" Everything inside the quotation marks is a string, and the string is called text.
2. Two ways to create a string:
3. Method of String:
Python2:
Python3:
Similarly, different version of the method of the string is not the same, the old routine, I still only parse common: Capitalize,casefold.center,count,decode,encode,endswith,expandtabs,find, Format,index,isalnum,isalpha,isdigit,isidentifier,islower,isspace,istitle,isupper,join,ljust,lower,lstrip, Partition,replace,rfind,rindex,rjust,rpartition,rpslit,rstrip,split,splitines,startwith,strip ', Swapcase, title, Translate, upper, zfill (not a bit more, yes, because strings are most commonly used in real-world development, so methods are used more)
Captalize: capitalizes the first word of the entire string and returns
Note, however, that the string is immutable , so it does not change itself
Casefold: Change all characters of the entire string to lowercase, Python3 Ritter some
Center (width): Centers The string and fills the new string with the width of length with a space
Count (sub[, start[, end]]): Returns the number of occurrences of a sub within a string, the start and end parameters indicate the range, the previous collection, and so on, and no longer demonstrates
Decode (decode= ' strict '): decodes a known encoding into Unicode, as explained earlier, and no longer has this method in Python3
Encode (encoding= ' utf-8 ', errors= ' strict '): encodes A string in the encoded format specified in encoding, as explained earlier
In Python3 do not need decode again encode, direct use encode:
EndsWith (sub[, start[, end]): Checks if the string ends with a sub substring and returns False if True. The start and end parameters represent ranges, which are optional
StartsWith (prefix[, start[, end]): Checks whether the string starts with prefix, returns TRUE, or False. The start and end parameters allow you to specify a range check, optional
Expandtabs ([tabsize=8]): Converts the tab symbol (\ t) in a string to a space, if no argument is specified, the default number of spaces is tabsize=8
This can not see the effect, self-operation experience it
Find (sub[, start[, end]): Detects if the sub is contained in a string, returns an index value if any, otherwise returns the -1,start and end parameters to indicate the range, optional
RFind (sub[, start[, end]): Similar to the Find () method, but looks from the right
Foramt: Formatted string (see 6th below)
Index (sub[, start[, end]): The same as the Find method, but if the sub is not in a string an exception is generated
Rindex (sub[, start[, end]): Similar to the index () method, but starting from the right
Isalnum (): Returns True if the string has at least one character and all characters are letters or numbers, otherwise False
The former because it contains commas and spaces, so false
Isalpha (): Returns True if the string has at least one character and all characters are letters, otherwise False
The former contains spaces
Isdecimal (): Returns True if the string contains only decimal digits, otherwise False, which is python3 specific
IsDigit (): Returns True if the string contains only numbers, otherwise False
IsNumeric (): Returns True if the string contains only numeric characters, otherwise False, same as the IsDigit method above, no longer demonstrates
Islower (): Returns True if the string contains at least one case-sensitive character, and the characters are lowercase, otherwise False
Isspace (): Returns True if the string contains only spaces, otherwise False
Istitle (): Returns True if the string is heading (all words start with uppercase and the remaining letters are lowercase), otherwise False
Isupper (): Returns True if the string contains at least one case-sensitive character and the characters are uppercase, otherwise False
Join (sub): Inserts a string as a delimiter between all the characters in a sub
Ljust (width): Returns a left-aligned string and fills the new string with a width of length using a space
Lower (): Converts all uppercase characters in a string to lowercase.
Partition (sub): Find substring sub, divide the string into a 3-tuple (pre_sub, Sub, fol_sub) and return if the string does not contain a sub (' Original string ', ' ', ')
Rpartition (sub): Similar to the partition () method, but looks from the right
Replace (old, new[, Count]): Replace the old substring in the string with the new substring, if count is specified, replace no more than count times
Rjust (width): Returns a right-aligned string and fills the new string with a width of length using a space
Split (Sep=none, maxsplit=-1): The default is a space-delimited slice string without parameters, if the Maxsplit parameter has a setting, only maxsplit substrings are delimited, and the list of substring concatenation after the slice is returned.
Splitlines ([keepends]): Returns a list containing rows as elements, separated by ' \ n ', and returns the first Keepends row if the Keepends parameter is specified
Strip ([chars]): Remove all whitespace from the front and back of the string, chars parameters can be customized to delete the character, optional
Lstrip (): Remove all spaces to the left of the string
Rstrip (): Removes the space at the end of the string (to the right).
Swapcase (): Flips the case in a string
Title (): A string that returns the caption (all words are started in uppercase and the remaining letters are lowercase)
Translate (table): Based on table rules (can be customized by Str.maketrans (' A ', ' B '), the Maketrans method is Python-specific, with less) convert characters in a string
Upper (): Converts all lowercase characters in a string to uppercase
Zfill (width): Returns the width of the string, the original string right-aligned, the front with 0 padding
4. A string can also be considered an immutable list
What do you say? Please see:
5. Strings can also be sliced
6. String concatenation to String formatting
string concatenation is the concatenation of multiple string objects with the symbol "+".
However, different types of objects cannot be spliced, such as:
In fact, there is a problem, with + splicing is a bit of evil, why is called evil, such as:
Or This example, have you found that "Yangi" is linked together, but you can:
Manually put a comma in front, but if you want to deal with a large amount of data, so it is cumbersome to manually, the development process will take time to engage in the lack of technical content of the problem is the root of the evil
So, in another way, formatted, formatted string is the most commonly used, formatted in the data type mentioned a little bit, said, as long as the "%" symbol encountered a string, this symbol is no longer the remainder. See below for specific symbols and corresponding meanings:
%c: Formatted character and its ASCII code%s: formatted string%d: Formatted integer%o: Formatting unsigned octal number%x: Formatting unsigned hexadecimal number%x: Formatting unsigned hexadecimal number (uppercase)%f: Formatting a floating-point number to specify the precision after the decimal point E: Format floating-point numbers with scientific notation%e: function with%e, format floating-point numbers with scientific notation%g: Depending on the size of the value to use%f or%e%g: function with%g, depending on the size of the value to use%f or%e
Example: print "I love Python" in a variety of ways
When it comes to this, you have to put the string formatting Related:
Formatting operator Auxiliary Commands:
* define width or decimal point accuracy-used for left alignment + display plus sign (+) in front of positive numbers <sp> spaces before positive numbers \# 0 (' 0 ') in front of octal digits, ' 0x ' or ' 0X ' in front of hexadecimal ( Depending on whether the ' X ' or ' x ' is used, the number shown in front of 0 is filled with ' 0 ' instead of the default space% ' percent ' output a single '% ' var ' mapping variable (dictionary parameter) M.N. M is the minimum total width displayed, and n is the number of digits after the decimal point (if available)
Baptism Soul, cultivation Python (9)--Spiritual string