Python study notes - string

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/sinat_34611224/article/details/84561244

Personally feel that it is a string of more flexible data format, it can do a lot of operations, also saw a lot of information, there was still need to use the time to look up a document, what the method is good. Here are some data compilation for future reference.

Python Strings

Python has a "str" ​​the built-in string class called, it has many handy features (there is a "string" called the old module, not the best). Although the more common single quotes, but can be a string literal double quotes or single quotes.

Backslash escapes work in single and double quotes text in the usual way - such as \ n, ', \ "(I did not do the" double-quoted strings of text can contain a single quote without any fuss, for example). " the same single quote string can contain double quotes. string literals can span multiple lines, but there must be a backslash at the end of each line \ to escape newline characters. a string literal triple quotes "" "or '' ' you can span multiple lines of text.

Python strings are "immutable", which means they can not be changed (Java string also use this immutable style) after it is created. Because the string can not be changed, a string representing the calculated value is to construct a new string. Thus, for example, the expression ( 'hello' + 'there') receiving the second string 'hello' and 'there' and construct a new string 'hellothere'.

String Methods

The following are some of the most common methods of string. The method is like a function, but it is "run" on an object. If the variable s is a string, then the code s.lower () on the lower running string object () method and returns the result (the idea of ​​the method is running on one of the objects constituting the basic idea of ​​Object - Oriented object-oriented programming, OOP). The following are some of the most common methods of string:

  • s.lower(), s.upper() – returns the lowercase or uppercase version of the string
  • s.strip() – returns a string with whitespace removed from the start and end
  • s.isalpha()/s.isdigit()/s.isspace()… – tests if all the string chars are in the various character classes
  • s.startswith(‘other’), s.endswith(‘other’) – tests if the string starts or ends with the given other string
  • s.find(‘other’) – searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not found
  • s.replace(‘old’, ‘new’) – returns a string where all occurrences of ‘old’ have been replaced by ‘new’
  • s.split(‘delim’) – returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it’s just text. ‘aaa,bbb,ccc’.split(’,’) -> [‘aaa’, ‘bbb’, ‘ccc’]. As a convenient special case s.split() (with no arguments) splits on all whitespace chars.
  • s.join(list) – opposite of split(), joins the elements in the given list together using the string as the delimiter. e.g. ‘—’.join([‘aaa’, ‘bbb’, ‘ccc’]) -> aaa—bbb—ccc

String Slices

"Slice" is a reference to a convenient method of syntax sequence of subparts - usually a string and list.
Slice s [start: end] is started from the beginning to the end of the extension element, but does not include.

String %

Python has a similar printf () is a combined tool string. % Type of operator acquired printf format string on the left (% d int,% s string,% f /% g floating point), the matching value in the right tuple (a tuple consisting of values ​​separated by commas usually grouped in brackets):

  # % operator
  text = "%d little pigs come out, or I'll %s, and I'll %s, and I'll blow your %s down." % (3, 'huff', 'puff', 'house')

More operations

keep updating…

References

  1. http://www.runoob.com/python3/python3-string.html
  2. https://www.jb51.net/article/151093.htm

Guess you like

Origin blog.csdn.net/sinat_34611224/article/details/84561244