Python Basic Tutorial Learning Notes---(3) string

Source: Internet
Author: User

1. Formatting strings %s represents a formatted string, or a placeholder operation for a string. If a variable is not a string by itself, it is automatically converted to a string. use%f to format floating-point numbers. It can also provide the required precision, such as%.3f, which preserves 3 decimal places. 2. Format:(1) Type of formatting:%s represents a string,%d represents a decimal integer, and%f is a floating-point number. The conversion can also set the field width and field precision. The field width refers to the minimum number of characters retained by the converted value, and the field precision is the number of decimal digits that should be included in the result. if the number of characters is insufficient, a space is used to occupy the placeholder. You can use * (asterisk) as the field width or precision, two can use * at the same time, the specific value of width and precision will be read from the corresponding parameter in the tuple. identifiers: You can also place an identifier in front of the width and precision, with an identifier of 0, plus, minus, and space. The identifier uses 0 o'clock, which means that 0 is populated when there is an empty space in front of it. when an identifier uses a plus sign, it indicates that the symbol is marked regardless of whether the result is negative. when the identifier uses a minus sign, the result will be left-aligned to the value. when an identifier uses white space, it means that spaces are padded to the left. when using the identifier 0 o'clock, 010 does not refer to octal, but the vacancy is replaced with 0, with a width of 10 (the minimum character length is 10). 3. String Method(1) Find methodThe Find method is used to find substrings in a longer string. It returns the first index where the substring is located. If it is not found, it returns-1. If there are multiple substrings, the index value that returns the start character of the first substring. You can also set two parameters in the Find function, that is, specify a lookup range. The starting index value position at which to terminate the index value location. The lookup scope contains the starting index value location, but does not contain the terminating index value location. you can allow only the starting index parameter value to be provided, but not only the terminating index value. (2) Join method The Join method is used primarily for connection strings and can only be used for connection strings. Uses one of the specified characters to concatenate multiple strings and finally returns a new string. multiple strings that need to be connected can be given once in the form of a list or tuple. (3) Lower methodlower is used to convert all strings to lowercase letters. Note: The upper () method is used to convert all strings into uppercase letters, the title () method is to convert the string to uppercase, other lowercase forms. (4) Replace methodThe replace method is used to replace all occurrences of a string with the specified string, and returns the substituted string. (5) Split methodis the inverse of join, in which a join is a new string with a specified symbolic connection, and split is a string that is split into multiple strings by a specified character and returned in the form of a list. Note: The split method and the join method have a different point, namely:connector. Join (string sequence)sequence of strings. Split (splitter)(6) Strip methodThe strip method is primarily used to return a new string after removing the space between two paragraphs of the string, but does not remove whitespace inside the string. You can also specify a string to remove two paragraphs, but be sure to both ends of the string.     

Python Basic Tutorial Learning Notes---(3) string

Related Article

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.