#! /usr/bin/python
#-*-Coding:utf-8-*-
#date: 20180501
Python data type
Integral type: int
Strings: String
1, the string once created will not allow the modification, so-called for loop to get the creation of a new string
Create
' Hello World '
Delete
del str_01,str_02
Stitching
#1. Use "+" to stitch two stringsstr_01 ='Hello'str_02=' World'New_str= str_01 +str_02Print(NEW_STR)>>>helloworld#2, through join. () method (only one sequence in parentheses)str_01 ='Hello'str_02=' World'List_str=[Str_01,str_02]new_str='****'. Join (LIST_STR)Print(NEW_STR)>>>hello****world" "Note: Through the "+" splicing, the disadvantage is to waste memory, not recommended, Python string in the C language is represented as a character array, each time you create a string need to open up a contiguous space in memory, and once you need to modify the string, you need to open up again, the evil of the + Each time it appears, it will re-open a space within it. " "
slices
' Hello World ' Print (str_01[0:]) # gets the first to last string, the first character is calculated from zero Print (str_01[:]) # get the entire string Print (str_01[-3:]) # starting from the last third character to the final character is Rld Print (Str_01[3:7]) # gets the fourth character beginning to the sixth character that is Lo W
Split
#simple Split method split, can not do multiple conditional segmentation, specify the characteristics of the separator to slice the string to split into a listPhone_number ='400-300-200-1234'Print(Phone_number.split ('-'))>>>[' + ', ' + ', ' $ ', ' 1234 'Print(Phone_number.split ('-', 2))>>>[' + ', ' + ' , ' 200-1234 '" "Note: Split is a left-to-right, delimited, slice rsplit is a right-to-left, delimited, slice splitlines is separated by rows (' \ r ', ' \ r \ n '), returns a list containing the rows as elements, if the parameter Keepe NDS is False, does not contain newline characters, and if true, line breaks are preserved. " "str1='AB C\n\nde fg\rkl\r\n'Print(Str1.splitlines ())>>>[' ab C ', ', ' de fg ', ' KL 'STR2 ='AB C\n\nde fg\rkl\r\n'Print(Str2.splitlines (True))>>>[' ab c\n ', ' \ n ', ' de fg\r ', ' kl\r\n ']#Complex Segmentation#R means no escaping, the delimiter can be, or, a space followed by 0 additional spaces, and then the pattern to split#\ Escape Character#\ nthe line break#\v#\ t Tab tab#\ r Enter#\s matches any white space character#R or R outputs the original meaning of the string, i.e. R ' \ n '-->\nImportreline='Hello World; Python, I, like, it'Print(Re.split (R'[;, s]\s*', line))>>>[' Hello World ', ' python ', ' I ', ' like ', ' it '
#正则表达式中 * Indicates a match for the preceding character 0 or more times
Formatting
Print("%s python is%s ok!"%("my","Me"))#>>>my python is me ok!#另一种格式化表达方式, with {} and: instead of% #通过方法str. Formart (), support position not in sequential orderPrint("{}{}". Format ("hello\t"," China"))#>>>hello ChinaPrint("{1} {0} {1}". Format ("Hello"," China"))#>>>china Hello China#Configurable ParametersPrint("Site name: {name}, Address: {URL}". Format (name="Learn python", url="www.baidu.com"))#>>> Site Name: Learn python, address: www.baidu.com
Note:%s represents a string
%d indicates a number
%f represents floating point type
processing of the beginning and end of a string
processing of the beginning and end of a string" "For example, we want to find out what the name of a file starts with or what it ends up with: Str.startswith () Str.endswith ()" "file_name='Hello World2018.txt'Print(File_name.startswith ('H'))#>>>falsePrint(File_name.endswith ('txt'))#>>>true
Finding and matching strings
"' General Find we can easily find substrings within a long string, returning the index of where the substring is located, if it cannot find the return-1'print (File_name.find (' ll '))> >>2Print (file_name.find (' ld '))>>>9' complex find ' t_date = ' 2018/05/12 ' Import Reif re.match (R ' \d+/\d+/\d+ ', t_date): print (' Ok,match ') Else: Print (' No March ')>>>ok,match
Note: A + regular expression that matches the preceding character one or more times
Substitution of strings
"""General Replacement Method: Str.replace ()"""str_04='Python is a easy language,but if you want to learn it, you should keep up write code everyday!'str_new= Str_04.replace (' Easy','Difficult')Print(Str_new,'\ n', Str_04)#>>>python is a difficult language,but if you want to learn it, you should keep up write code everyday!#Python is a easy language,but if you want to learn it, you should keep up write code everyday!"""complex need to handle multiple replacement methods: Re.sub ()"""My_home='Man , Girl'Print(Re.sub (R'\d+',' -', my_home))#>>>man, Girl
Data types of Python learning