Python----------string

Source: Internet
Author: User

String * Number-------------print string number of times

"Echo" * 3 ' Echoechoecho '

  

Strings are added--------------strings are joined together

s = ' Hello ' + ' world's ' Hello World '

String length---------------------len

Split: S.split

S.split () splits s by a space (including multiple spaces, tabs \t , newline characters, \n etc.) and returns all the segmented strings.

ine = "1 2 3 4  5" numbers = Line.split () print numbers[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']

"1,2,3,4,5" line.  Split(', ')numbers
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
Connection: S.join
s = ' S.join (numbers) ' 1 2 3 4 5 ' s = ', ' S.join (Numbers) ' 1,2,3,4,5 '
Replacement: S.replace
s = "Hello World" s.replace (' World ', ' python ') ' Hello python '
Case conversion:

The S.upper () method returns a new string that capitalizes all the letters in S.

The S.lower () method returns a new string that will all lowercase letters in s.

"Hello World". Upper () ' Hello world ' #这两种方法也不会改变原来s的值s = "Hello World" Print s.lower () print Shello Worldhello World
Remove extra spaces:

S.strip () returns a new string that removes extra spaces at both ends of S.

S.lstrip () returns a new string that is removed from the extra space that begins with S.

S.rstrip () returns a new string that removes the extra space at the end of S.

s = "  Hello World   " S.strip () ' Hello World '
Multi-line string:

Python uses a pair """ or ‘‘‘ to generate a multiline string:

A = "" "Hello world.it is a nice day." "" " Print Ahello world.it is a nice day.

When storing, we add a line break between two lines of characters‘\n‘

Use ()Or \To change the line:

When the code is too long or for the sake of aesthetics, there are two ways to convert a line of code to multiple lines of code:

    • ()
    • \
A = ("Hello, World.")    It ' s a nice day. "    My name is xxx") a "Hello, world." It ' s a nice day. My name is xxx "


"Hello, world."     " it ' s a nice day."     "My name is xxx"a
Hello, world. It ' s a nice day. My name is xxx "
Cast to String:

    • str(ob)Coercion is ob converted into a string.
    • repr(ob)is also coerced into a ob string.

The different points are as follows:

STR (1.1 + 2.2) ' 3.3 ' repr (1.1 + 2.2) ' 3.3000000000000003 '
Formatted string: Format ()
' {} {} {} '. Format (' A ', ' B ', ' C ') ' A B C '

You can specify the relative position of the incoming parameter with a number: ' {2} {1} {0} '. Format (' A ', ' B ', ' C ') ' C b a '

You can also specify the name of the incoming parameter: ' {color} {n} {x} '. Format (n=10, x=1.5, color= ' Blue ') ' Blue 10 1.5 '

Can be mixed together: ' {color} {0} {x} {1} '. Format (ten, ' foo ', x = 1.5, color= ' blue ') ' Blue 1.5 foo '

You can use {<field name>:<format>} to specify the format: from Math import pi ' {0:10} {1:10d} {2:10.2f} '. Format (' foo ', 5, 2 * pi) ' Foo 5 6.28 ' specific rules are the same as in C.

You can also use the old-fashioned% method for formatting:
s = "Some numbers:" x = 1.34y = * is separated by a percent, enclosed in parentheses t = "%s%f,%d"% (s, x, y) t ' some numbers:1.340000, 2 '

  

Python----------string

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.