Python base common statements and strings

Source: Internet
Author: User

Common Statements :

If, elif, else: Select conditional statement

Format:

If condition:

Pass

Elif Conditions:

Pass

Else

Pass

For: Sequence iterations
A Python for loop can traverse any sequence of items, such as a list or a string

1  forIinch 'python':2     Print(i)3 #Results4 P5 y6 T7 h8 o9 NTen  forIinchRange (5): One     Print(i) A #Results - 0 -1 the2 -3 -4
View Code

Pass is an empty statement in order to maintain the integrity of the program structure.
Pass does not do anything, generally used as a placeholder statement.
Break: Exit Loop
Continue: Skips the current loop, loops continue

String

String formatted output
name = "Python"
Print "I am%s"% Name
#输出: I am python
PS: string is%s; integer%d; floating-point number%f

A= ' Hello ' b= ' Python '
+: String connection

>>>a + B ' Hellopython '
*: Duplicate output string

>>>a * 2 ' Hellohello '
[]: Getting characters in a string by index

>>>A[6] ' e '
[:]: Intercepting part of a string

A[0:3] ' Hel '
In: Member operator-returns TRUE if the string contains the given character

>>> "E" in a

True

String built-in functions:

String.count (str, beg=0, End=len (String))
Returns the number of occurrences of STR in a string, if beg or end specifies that the number of STR occurrences in the specified range is returned
String.find (str, bg=0, End=len (String)) The Find method can detect if STR is contained in a string, and if BG and end develop scopes, check whether it is contained within the specified range, or return the starting index value if it is, or 1.
String.strip ([obj]) truncates the trailing space at the beginning of string strings

String method:

String.count (str, beg=0, End=len (String))
Returns the number of occurrences of STR in a string, if beg or end specifies that the number of STR occurrences in the specified range is returned
String.find (str, bg=0, End=len (String)) The Find method can detect if STR is contained in a string, and if BG and end develop scopes, check whether it is contained within the specified range, or return the starting index value if it is, or 1.
String.strip ([obj]) truncates the trailing space at the beginning of string strings

String method Join () and split ()--Link and split
A join (sub) is inserted between all the characters in a sub with a string as a delimiter
Split (sep=none,maxsplit=-1) without parameters by default is a space-delimited slice string, if the Maxsplit parameter has a set, then only maxsplit substrings are split, returning the list of sub-string concatenation after slicing

Isx method

isdigit () returns True if the string contains only numbers, otherwise false
isspace () returns True if the string contains only spaces, otherwise false
istitle () returns True if the string is a caption (all words start with uppercase and the remaining letters are lowercase), otherwise false
Upper () converts all lowercase characters in a string to uppercase
Lower () converts all uppercase characters in a string to lowercase

Python base common statements and strings

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.