Python basic common statements and strings

 

Common phrases :

if, elif, else: select conditional statements

Format:

if condition:

  pass

elif condition:

  pass

else:

  pass

for: sequence iteration
Python for loops can iterate over any sequence of items, such as a list or a string

 

1  for i in  ' python ' :
 2      print (i)
 3 #result  4 p 5
 y 6
 t 7
 h 8
 o 9
 n 10
 for i in range (5 ):
 11 print (i
 ) 12 #result 13 0
 14 1
 15 2
 16 3
 17 4
             
 
View Code

 

 

pass is an empty statement to maintain the integrity of the program structure.
pass does nothing and is generally used as a placeholder statement.
break: exit the loop
continue: skip the current loop, the loop continues

 

string

String formatted output
name = "python"
print "i am %s " % name #output
: i am python
PS: string is %s; integer %d; float %f

a='Hello' b='Python'
+ : string concatenation

>>>a + b 'HelloPython'
* : repeat the output string

>>>a * 2 'HelloHello'
[]: Get the characters in the string by index

>>>a[6] 'e'
[:]: intercept part of the 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 the string, if beg or end is specified, it returns the number of occurrences of str within the specified range
string.find(str, bg=0, end=len(string)) The find method can detect whether str is included in the string. If bg and end specify a range, check whether it is included in the specified range. If so, return the starting index value, otherwise return -1.
string.strip([obj]) truncates the spaces at the beginning and end of the string string

String method:

string.count(str, beg=0, end=len(string))
returns the number of occurrences of str in the string, if beg or end is specified, it returns the number of occurrences of str within the specified range
string.find(str, bg=0, end=len(string)) The find method can detect whether str is included in the string. If bg and end specify a range, check whether it is included in the specified range. If so, return the starting index value, otherwise return -1.
string.strip([obj]) truncates the spaces at the beginning and end of the string string

String methods join() and split() - linking and splitting
join(sub) uses a string as a delimiter and inserts between all characters in sub
split(sep=none,maxsplit=-1) without parameters by default Slice the string with space as the delimiter. If the maxsplit parameter is set, only maxsplit substrings are split, and the list of spliced ​​substrings after slicing is returned.

 isX method

isdigit() returns true if the string contains only digits, false otherwise
isspace() returns true if the string contains only spaces, false otherwise
istitle() if the string is titled (all words are in uppercase start, the rest of the letters are lowercase), return true, otherwise return false
upper() converts all lowercase characters in the string to uppercase
lower() converts all uppercase characters in the string to lowercase

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339158&siteId=291194637