How to Learn: Python learning path (4): String strings are also Sequences
The reason why I say this, that is to say, the sequence operation, strings are also available.
Format a string
I have special feelings for formatting strings, because a long time ago, when I learned the first C language, when I learned the Book of Tan haoqiang, how much time have I spent remembering the number of formatted characters. I forgot about it now. In many cases, we have learned a lot of useless things, so many times we have to remember the key points. If you want to use these formats, check them. Do you have any problems? What's more, this is a blow to others' confidence.
The Code is as follows:
- Print "int: % d, string: % s" % (33, "String ")
The result is as follows:
- Int: 33, string: String
Use % d and % s to hold the space in the string to be printed, and add % () to the end of the entire string.
% D is an integer placeholder, and % s is a string placeholder.
One day, this table won't save you any more. is searching the easiest way?
Method
Python has the most convenient help documentation, so there is no need to demonstrate methods one by one.
Source code:
- A = "str"
- Print dir ()
Result:
- ['_ Add _', '_ class _', '_ ins INS _', '_ delattr __', '_ doc _', '_ eq _', '_ format _', '_ ge _', '_ getattribute __', '_ getitem _', '_ getnewargs _', '_ getslice _', '_ gt _', '_ hash __', '_ init _', '_ le _', '_ len _', '_ lt _', '_ mod __', '_ mul _', '_ ne _', '_ new _', '_ reduce _', '_ performance_ex __', '_ repr _', '_ rmod _', '_ rmul _', '_ setattr _', '_ sizeof __', '_ str _', '_ subclasshook _', '_ formatter_field_name_split', '_ formatter_parser', 'capitalize', 'center', 'Count ', 'decode', 'enabled', 'enabledwith', 'pandtabs ', 'Find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit ', 'islower ', 'isspace', 'istitle', 'isupper', 'join', 'ljust ', 'lower', 'lstrip', 'partition', 'replace ', 'fappin', 'rindex', 'partition ust ', 'rpartition', 'rsplits', 'rdstrip', 'splits ', 'splitlines', 'startswith', 'strip ', 'swapcase', 'title', 'translate', 'uppper ', 'zfill']
Common methods are as follows.
- A = "str"
- Help (a. find)
-
- Help on built-in function find:
-
- Find (...)
- S. find (sub [, start [, end])-> int
-
- Return the lowest index in S where substring sub is found,
- Such that sub is contained within S [start: end]. Optional
- Arguments start and end are interpreted as in slice notation.
-
- Return-1 on failure.
The first parameter is the string you want to search for. It is optional for where to start and where to end the search.
The returned value is an integer. If the returned value is-1, the search fails.
- A = "strtrtr"
- Help (a. replace)
-
- Help on built-in function replace:
-
- Replace (...)
- S. replace (old, new [, count])-> string
-
- Return a copy of string S with all occurrences of substring
- Old replaced by new. If the optional argument count is
- Given, only the first count occurrences are replaced.
The first of the three parameters is the string to be replaced, the second is the string to be replaced, and the third is the replacement of the first matching in the old string.
I will not go into details here.