Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1, python string type, keyword STR; Definition: Pairs of single and double quotes are strings

Keyword: STR string int Integer float floating point tuple dict dictionary list List

str_2="lemon python class "
str_1='PythonAotuTest'
print(type(str_1))
Copy the code

2. The value of a string is ordered. The string is composed of elements one by one and has an index, starting from 0, or the reverse order starting from -1

Print (" take the string variable's value: ", str_2) print (" positive values: "str_2 [2]," reverse value: ", str_2 [16])Copy the code

The special character \n \t \r is escaped by adding r/ r or backslash \ before the string, which is the normal string output \n

str_2=r"python\nclass" 
print(str_2)
Copy the code

String operation + concatenation, * multiplication. The priority; Support the same type + splicing: different types of splicing, need to convert, forced. That is, to force the original type into concatenation of the same type

# print (str_2 + str_1 * 4) # I = (1, 2, 3) # # j = 111 print (STR (j) + str_1) # print (STR) (I) + str_1Copy the code

5, member operator: in not in

Print (" member operator, condition: ",' I 'not in str_2,'o' not in str_2)Copy the code

6. Slice valuem:n:kStart location: Destination Location: Step size (equal difference) The default value is 1. The value is left and not right. N-1 End value

Print (" print ") print(" print ") print(" print ") print(" print ") print(" print ") print(" print ") print(" print ") print(" print ") print(" print ") print(" print ") print(" print ") ", str_2 [1:16:3], ", the support section order: "str_2 / - 16:3:1) # is [- 16:] can order # print (str_2 [2] : :)Copy the code

7, format output,%s string (can put any type of value) %d integer %f floating point,%.2f retain two decimal places

# format {} placeholders can be used without position, default order, index starting from 0, if the index value, age=19 name='python' print(" format: {1} {0} ". The format (the age, name), "the placeholder: output % s: % s" % (str_2, age))Copy the code

8. Use of built-in functions

# change case: Upper (), lower() print(" upper :",str_2.upper(), str_1.lower()) # swapCase () print(str_1.swapcase()) # find( Print (" index:",str_2.find("t"),"," Print (str_1," replace(",str_1.replace("t"," t", 2)) # split() Str_4 =str_2.split("n") print(str_2," str_2.split("n")," ",type(str_4), ", but the element \'{}\' is still a string: ". The format (str_4 [2]), type (str_4 [2])) # strip head and tail character elements () print (str_2, string element contrast: before and after processing, str_2. Strip (" l ") # length function __len__ () Print (" return string str_2 length: ",str_2.__len__())Copy the code