Python Basics-string

Source: Internet
Author: User

String 1, Form

  string enclosed in single quotation marks:' Hello '

string enclosed in double quotes:"Hello"

string enclosed in three quotes:"' Hello '" ( three single quotes ),"" "" Hello "" "(three double quotes)

Strings enclosed in three quotes can be wrapped

2. SubscriptIndex

From left to right , subscript starting from 0;

From right to right , subscript starts from-1

' ABC ' Print (A[0])  # # Output Result: a Print # # Output Result: C

3, slicing--equivalent to the interception of strings

Syntax: string [ start index : End index : step ]--- character with no end index

List, Ganso, string all have slicing operation!!

Format: info[start index: End index]
Characters that do not include the end index

1info ='Life are short, you need Pyhton'2 Print(Info[0:4]) 3 Print(info[5:])## 5 to end character4 Print(Info[:10])#start to 10 position5 Print(Info[1:10:2])## 2 indicates step size6 Print(Info[::2])##隔一个取一个7 Print(Info[::-1])## Reverse!!!
Common Operations for strings:

No matter how the string is manipulated, the original string must be constant!! Typically, a new string is generated

  The immutability of strings!!

1, strip ()----Remove space

1 '  '2print(A.strip ())  ## Remove all spaces 3Print  ## Remove left space 4print## To remove the right space

2, Upper, lower---case conversion

1 ' AbCDE ' 2 Print (A.lower ())  # # The output is: ABCDE 3 Print # # Results of output: ABCDE 4 5 # # determine if uppercase or lowercase:6 isupper () and Islower ()

3, EndsWith () startswith ()---judged by the beginning of XX or with the end of XX

1 ' asd_234 ' 2 Print # #输出结果是: True 3 Print (B.startswith ('as'##输出结果是: True

4. Concatenation of Join () strings

1 Print("'. Join (['Hello',' World']))##输出的结果是: HelloWorld2 3 ##更加常用的拼接方法:4A ='ABC'5b ='efgr'6 Print(A+B)## The result of the output is: abcefgr

5, a variety of commonly used judgment numbers, letters, spaces

## Judging whether it's all numbers Print('123'. IsDigit ()) Print('123'. Isdecimal ())Print('123'. IsNumeric ())## Determine if it is a spacePrint(' '. Isapace ())## True## Determine if all characters are composedPrint('ABCDF'. Isalpha ())## Determine if there is a number and character compositionPrint('fff123'. Isalnum ())

Summary: The above several methods can be combined with a for loop, after the traversal to judge the situation of all characters, for the number of characters to count what

6. Split ()-----Split

The split result does not contain a delimiter, and the result returns a list

1 ' I Love You ' 2 Print (The result of the Name.split ('## # output is: [' I ', ' love ', ' You ']34#  # If the split character does not exist, simply give the string to the list 5Print(name.split ('x'  ##输出的结果是: [' I love You ']
1Small example: Suppose input: 10*20finding the results of calculations2Shuzi ='10-20'3 ifShuzi.find ('*')!=-1:##找到了 *4L = Shuzi.split ('*')## Split, return the result is: l = [' 10 '. ' []5v = Int (l[0]) *int (l[1])6     PrintV##输出的结果是:

There is also a split: Splitlines ()---split by line break!!

7. Replace ()-----replacement

After substitution, a new string is generated and the original string is unchanged

1 ## replaces all specified characters by default2info ='Life is shorts'3 Print(Info.replace (' is', was))##输出: Life is shorts4 5 ## Specify number of replacements6 Print(Info.replace ('s','S', 1))##输出: Life is shorts

8. Find ()-----Lookup

Returns the index of the first character if it exists;

  If it does not exist, return-1

1info ='Life are short, you need Pyhton'2 Print(Info.find (' is'))3 Print(Info.find ('III'))4 Print(Info.find (' is', 10,20))##指定范围5 6 ##index () is also a lookup, if not present, will report an exception7 Print(Info.index ('SSS'))8 ##指定范围9 Print(Info.index (' is', 4,20))Ten  One ## #获取指定的内容出现的次数 A ## # does not exist return 0 times - ## You can also specify a range - Print(Info.count ('s')) the Print(Info.count ('s', 10,15))

8. Format ()-----formatted string

    in the format function, use {} to act as a format operator

1 Print('{},{}'. Format ('Chuhao', 20))##chuhao,2 Print('{1},{0}'. Format ('Chuhao', 20))##20, Chuhao3 Print('{1},{0},{1}'. Format ('Chuhao', 20))##20, chuhao,20

Python Basics-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.