Python Basics (Str,list,tuple)

Source: Internet
Author: User
Tags first string

Python is a dynamically interpreted, strongly typed definition language (compiled and interpreted first)

Dynamic type language

When programming a dynamically typed language, you never have to specify a data type for any variable, and the language will record the data type internally when you assign it to a variable for the first time.

Explanatory type

Each execution of a program to the source program, there will be a shell called the interpreter to convert the source code into binary code for execution, in general, is to constantly explain, execute, interpret, execute

Strongly typed

Once a variable is assigned a data type, it is always the data type if it is not cast.

Advantages: High efficiency, team development, porting, expansion, embedding

Cons: Slow, no encryption, no multithreading parallel process

Tell the interpreter what code to execute

#!/usr/bin/env python # -*- coding: utf-8 -*-Binary binary 18 binary 12,345,670 binary 1,234,567,896 binary 0123456789ABCDEF Comment # single-line comment ' multiline comment ' ' User input:input(' User input content ') PRINT screen name = ' Sunlizhao '
   Print(' I am%s '% (name))%sis a string%dis a number%fis a floating-point data type numberint(integer), long (longer shape),float(floating point type), complex (negative) Boolean true or FALSE, 1 or 0,trueOrfalseThe string name = ' Sunlizhao ' Print (' I am%s '% (name))%s is the string%d is the number%f is a floating-point string operation: Name.capitalize () capital Letter Name.casefold () Uppercase all lowercase name.center (50, '-') output on both sides of name-character Name.count (' s ') counts the number of occurrences of S Name.encode () character encoded as bytes The Format name.endswith (' LZ ') determines whether the string is the LZ end ' S\tlz '. Expandtabs (10) say \ t convert to how long the space Name.find (' SL ') Find SL, Find returns its index, find not To return-1
Format: >>> msg ="My name is {}, and the is {}">>> Msg.format ("Alex", 22)‘My name is Alex, and are 22' >>> msg ="My name is {1}, and age is {0}">>> Msg.format ("Alex", 22)‘My name is a, and age is Alex' >>> msg ="My name is {name}, and age was {age} ">>> Msg.format (Age=22,name=" ale" ) my name is the ale, and age is 22 '  Format_map >>> msg.format_map ({ ' name ": " alex  ",  ' age": 22})  ' my name Is Alex, and age is 22         
NUM1 = [11,22,33]num2 = (11,22,33) #num3 = {' A ' = one, ' b ' = $, ' c ' = ' mystr ' = ' Hello word itcast and ' #字符串操作print (mystr ) Print (mystr.Find("word")) #找不到返回-1, locate return subscript print (mystr.RFind("Itcast")) #从右面查找, return to the following table print (MyStr.Index(' word ')) #同find, the exception print (MYSTR) is not found.Rindex("Itcast")) #同find Print (mystr.Count(' E ')) #查找Number of occurrencesPrint (mystr.Replace(' word ', ' word ', 1)) #将wordReplacePrint (MYSTR) for Word (one from left to right).Split(‘ ‘)) #将字符串中的空格CuttingDrop Print (mystr.Capitalize())#The first string of stringsCharacter uppercase print (mystr.title())#Capitalize first letterPrint (mystr.Lower())#all uppercaseCharacterTurnChange tolowercasePrint (mystr.Upper()) #所有lowercaseLettersTurnChange toUppercaseLyric = "Want to accompany you to see the Sea" Print (lyric.Center(50)) #在Center in 50 charactersPrint (lyric.Ljust(50)) #靠左print (lyric.Rjust(50)) #靠右 Print (lyric.Strip()) #清除左右Both sidesSpace Print (lyric.Lstrip()) #清楚leftSpace Print (lyric.Rstrip()) #清楚RightSpace Print (lyric.Partition("Together")) #以 'together ' as the centerScore ofcut into three segments .Print (Lyric.rpartition ("Together")) #从右边开始查找 (tuple) file_name = "Xxxx.txt" Print (file_name.EndsWith(". txt")) #判断是否是. txt tosuffixPrint (file_name.StartsWith("XXXX")) #判断是否是xxxx为StartNumbe = "Q" Print (Numbe.Isalpha()) #判断whether it is a letterPrint (Numbe.IsDigit()) #判断是否是DigitalPrint (Numbe.isalnum()) #判断是否是numbers or letters Print (Numbe.isspace()) #判断是No is a pure spaceA = ["AA", "BB", "cc"]B = "" Print ("". Join (a)) #将a中的字符串用b连接起来

list, meta-ancestor operations

definition List    names = [' Sun Lizhao ', ' slz ', ' Sunlizaho ']Subscript starting from 0 names[0],names[1],names[2] Rewind from-1 start names[-1],names[-2],names[-3] Slice: Take multiple elementsStep Sizeis 2 names[:: 2] If you want totake the last element, cannot write-1names[0:]Append: Fromlast addedNames.Append(' I'm new here ') insert: ThroughIndex InsertNames.Insert(0, ' I am the first ') Modified: byIndex ModificationNames[0] = ' I am the first 'Delete:Strong deletionListdefNamesClearList Names.Clear() in accordance withSubscript Delete element contents    defNames[0] Names[0] = []DeleteSpecifying elementsNames.Remove(' slz ') Delete listfinallyOnevalueNames.pop ()Expand:List1of the contentAdd to NamesOfbackNames.Extend(List1) Statistics:StatisticsThe number of occurrences of slz in names names.Count(' Slz ') sort/flip: According to ASCLL codeSort, note that py3 different data types cannot sort names.Sort() the entire listreverse (not row only pour)Names.Reverse() get subscript: Based on element contentGet subscript(only the first one found) is returned names.Index(' Slz ') tuple: Once a tuple is created, it must not be modified T = (A,b,[c,d]) TT = t[2]tt[0] = ett[1] = fprint (TT) Not that it can't change? Why is it changing now?!tuple is the samePoint to constant, the list you point to cannot be changed, but the contents of it can be changed.

  

Python Basics (Str,list,tuple)

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.