python_ string, a tuple formatted output

A String

1. The pair of strings are single or double quotes enclosed. For example: name = "Joe Smith", sex = "female"

2. String index is zero-based

3. Slice the string

a. single string value

---- string variable name [string index position]

b. a string of values

String variable name [character from the beginning: the character ending position +1]

c. The value is to take a slice of the left and right do not take

E.g. name = "helloword"

 

print (name [1: 5]) ---- acquired character string of the second to fourth, the result is output ello

4. The value of mode

For example: name = "Zhangsanlisi"

print (name [1: 3]) ---- value results for the three Lee

print (name [-1]) --- the result is four

print (name [1:]) ---- acquisition element 1 so as to end, the result is three John Doe

a. from left to right index value index starts from 0

b. from right to left below the value of the index is from -1 to start

Note: Whether the index is below 0 from the beginning or from the interception order from left to right are at -1, ie from small to large

[:: --1] reverse text

II. Formatted output

1.% d: Print integer

For example: age = 18 name = "John Doe"

print ( "My name is% s my age is% d"% (name, age))

2.% s: print string

Example: name = 'John Doe'

print ( "My name is% s"% name)

3.% f: print float

Example: ff = 3.1415927

print ( "The float is% f"% ff)

4. For example: age = 18 name = "John Doe"

print ( "My name is {0} my age is {1}". format (name, age))

5.r '': content output in the form of a string which does not need to escape is unnecessary to escape the single quotes

III. Tuple tuple

1. The key tuple is a tuple, the symbol is (), tuple elements are separated by commas

2. tuples which can not change the value of

For example: a = (1,2, "San", [ "John Doe", "Wang Wu"], { "aa", "bb"})

a [0] = 3 --- to the first element of the tuple is assigned by the assignment operator would be given

When the changes in the list of tuples which can not be modified, if the modification value inside a tuple list which may be modified.

3.type (variable name / value): Get value of variables or data type

Note: This is a pit: Example a = ( "John Doe")

Check only one tuple element ------ type requires a comma after the detection element ( "John Doe")

4. tuple slice index starting from 0

例a=(1,2,"张三",("aa","bb","cc"))

b=("a","b")

(A) ----- tuple outputs of all elements in a.print

b.print (a [2]) ---- output tuple third element, the output is "San"

c.print (a [1: 4]) --- the output of the second element group to the fourth element, the output is 2, "Joe Smith", ( "aa", "bb", "cc" )

d.print (a [1:]) --- Output the tuple to a second end of the element

e.print (a + b) ---- The a, b stitched together two yuan groups

f.print (a * 2) ---- The print twice a tuple

 

Guess you like

Origin www.cnblogs.com/msmx/p/10954684.html