Python string learning Notes

Source: Internet
Author: User

0X00 How to define a string

Python does not need to be defined .. In Python, strings are usually written in this way.
>>> Str1 = 'Hello, world'
>>> Str2 = "It's work"
>>> Str3 = "" Jack: "oh shit """"
Str1 statements are the most common statements.
The str2 declaration method can contain single quotation marks in the string'
The str3 declaration method can contain double quotation marks in the string, "and can also wrap in the string.
0X01 concatenated string

>>> Str1 = "hello"
>>> Str2 = ","
>>> Str3 = "world"
>>> Print str1 + str2 + str3
Hello, world
Note:
The plus sign is a string on both the left and right sides of the connection. If it is a string or a number, it will not work. Unless you convert a number into a string
0X02 input string

Standard input is to directly write your input into the code, or even use the variable name.
The original input is a string that is a pure string.
For details, see the following Demo.
>>> Hello = "hello, world"
>>> Str1 = input ("what's your name:") # get standard input
Hello
>>> Str2 = raw_input ("what's your name:") # obtain the original input
"Hello"
>>> Print str1
Hello, world
>>> Print str2
Hello
0X03 string formatting

After learning C, you can quickly understand the string formatting in Python. If you have not learned C, you can quickly understand the string formatting in Python.
>>> From string import Template
>>> Text = Template ("1 --- $ a 2 --- $ B 3 --- $ c 4 --- $") # $ a $ B $ c is a string placeholder, first write and then assign values
>>> Text. substitute (a = "hello,", B = "world", c = "wow ")
'1 --- hello, 2 --- world 3 --- wow 4 --- $'
# % S is a string placeholder. Add the following strings to the front.
>>> Print "hello, % s" % ("world ")
Hello, world
# % 15 s is to expand the string forward by 15 digits
>>> Print "hello, % 15 s" % ("world ")
Hello, world
>>> Print "% 15 s, world" % ("hello ")
Hello, world
#%-15 s is to extend the string back to 15 characters
>>> Print "%-15 s, world" % ("hello ")
Hello, world
0X04 string processing functions

Find finds another string from one string, returns the leftmost index, and returns-1 if not found.

>>> Str = "hello, world"
>>> Str. find (",")
    5
>>> Str = "hello, hello, world, world"
>>> Str. find (",", 8, 12)
11
If the find function returns 0, it is not found, but found at 0. After all, the programmer's world is always counted from 0.

Elements in join sequence

>>> Str1 = ['hello', 'World']
>>> Str2 = "---". join (str1)
>>> Print str2
Hello --- world
Note that str. join (list) is used instead of list. join (str)

Lower returns the string in lowercase.

>>> Str1 = "HELLO, WORLD"
>>> Str2 = str1.lower ()
>>> Print str2
Hello, world
Replace search and replace all

>>> Str1 = "my world, my house, my phone"
>>> Str2 = str1.replace ("my", "your") # I transferred everything to you.
>>> Print str2
Your world, your house, your phone
Split string

>>> Str1 = "hello, world"
>>> Str2 = str1.split (",")
>>> Print str2
['Hello', 'World'] # A list is returned.
Strip remove things on both sides

Strip removes spaces on both sides by default. You can also add parameters.
>>> Str1 = "hello, world"
>>> Str2 = str1.strip () # spaces are removed by default.
>>> Print str2
Hello, world
>>> Str1 = "aaahello, worldaaa"
>>> Str2 = str1.srtip ("a") # add a parameter to delete the content in the parameters on both sides.
>>> Print str2
Hello, world

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.