How to Learn: Python learning path (4): String

Source: Internet
Author: User
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:

  1. Print "int: % d, string: % s" % (33, "String ")

The result is as follows:

  1. 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:

  1. A = "str"
  2. Print dir ()

Result:

  1. ['_ 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.

  1. A = "str"
  2. Help (a. find)
  3.  
  4. Help on built-in function find:
  5.  
  6. Find (...)
  7. S. find (sub [, start [, end])-> int
  8.  
  9. Return the lowest index in S where substring sub is found,
  10. Such that sub is contained within S [start: end]. Optional
  11. Arguments start and end are interpreted as in slice notation.
  12.  
  13. 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.

  1. A = "strtrtr"
  2. Help (a. replace)
  3.  
  4. Help on built-in function replace:
  5.  
  6. Replace (...)
  7. S. replace (old, new [, count])-> string
  8.  
  9. Return a copy of string S with all occurrences of substring
  10. Old replaced by new. If the optional argument count is
  11. 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.

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.