程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning (III): String slicing & splitting & Merging & resident mechanism & String Method & variable string & basic operator & sequence & slicing operation & list

編輯:Python

Python Learn to share today :

String slice & Division & Merge & Resident mechanism & String method & Variable string & Basic operators & Sequence & Slicing operation & list

Let's look at the method first :






Case code :


# String slice slice operation ( Intercepting string )
a = "abcdef"
# The result of printing is :bcde, Array index from 0 Start 
# Format :[ Starting offset start: End offset end: step step]
print(a[1:5])
print(a[:3])
# In steps of 1 It's normal 
print(a[1:5:1])
# In steps of 2 Print one character for every two characters 
print(a[1:5:2])
# Reverse extraction 
print(a[::-1])
# split() Divide and join Merge 
# Use join Splicing is better than using “+” High splicing efficiency 
a = "w am x"
# Print the results :['w', 'am', 'x']
print(a.split())
b = a.split()
# Print the results :w*am*x
print("*".join(b))
# String resident mechanism and string comparison 
# String resident : The method of saving only one copy of the same and immutable string , Different values are stored in the string resident pool .Python Support string resident mechanism ,
# For strings that conform to identifier rules ( Contains only underscores (_), Letters and numbers ) The string persistence mechanism is enabled 
a = "aba_3"
b = "aba_3"
print(a is b)
c = "dd#?"
d = "dd#?"
print(c is d)
# Member operators 
# in/ not in keyword , Judge a character ( Substring ) Whether it exists in the substring 
a = "abc"
b = "a"
# The result is true
print(b in a)
# Summary of common methods of string 
a = " My test documentation as"
# String length 
print(len(a))
# Starts with the specified string 
print(a.startswith(" I "))
# Ends with the specified string 
print(a.endswith(" file "))
# The first occurrence of the specified string position 
print(a.find(" Of "))
# The last occurrence of the specified string 
print(a.rfind(" Of "))
# The specified string appears several times 
print(a.count(" I "))
# All characters are letters or numbers 
print(a.isalnum())
# All capital letters 
print(a.upper())
# All lowercase letters 
print(a.lower())
# Remove the space between the beginning and the end 
print(" sex ".strip())
# Remove the left *
print("*sex*".lstrip("*"))
# Remove the one on the right *
print("*sex*".rstrip("*"))
# Format layout 
a = "sex"
# In the middle The result is :***sex****
print(a.center(10, "*"))
# be at the right The result is : *******sex
print(a.rjust(10, "*"))
# be at the left side The result is :sex*******
print(a.ljust(10, "*"))
# character string format format 
a = "name={0},age={1}"
print(a.format(" Bigfoot ", 11))
# ^ . < . > They're centered , Align left , Right alignment 
print(" I am a ..., I like numbers {0:*^8}".format("666"))
# Variable string 
# The import module 
import io
s = "hekko"
sio = io.StringIO(s)
# Take value 
print(sio.getvalue())
sio.seek(2)
sio.write(" Shirley ")
# The result of printing is : he Shirley ko
print(sio.getvalue())
# Basic operators 
a = 3
# The result of printing is 24 1 Multiplication and division 
print(a << 3)
print(a >> 1)
# Sequence 
# Sequence is a way to store data , Used to store a series of data , In memory , Sequence 
# It is a continuous memory space used to store multiple values 
# python The size of the list is variable , Increase or decrease as needed 
# List creation 
# Basic grammar [] establish 
a = [100, 200, " details "]
# Create an empty list object 
b = []
b.append(200)
print(b)
# list() establish 
a = list()
a = list(range(10))
# The result of printing is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a)
a = list("w")
# The result of printing is : ['w']
print(a)
# adopt range() Create a list of integers 
# range( Start number , Ending number , step )
print(list(range(0, 5, 1)))
# Generate list by derivation 
a = [x*2 for x in range(5)]
# The result is :[0, 2, 4, 6, 8]
print(a)
# adopt if Filter element 
print([x*2 for x in range(100) if x%9 == 0])
# Addition and deletion of list elements 
# When elements are added and removed from the list , The list automatically manages memory , Greatly reduce the burden of programmers . But this feature involves a lot of movement of list elements , Low efficiency , Unless necessary ,
# We usually only add or delete elements at the end of the list , This will greatly improve the operational efficiency of the list 
# append() Method 
# Modify the list object in place , Is to add a new element at the end of the real list , The fastest , Recommended 
a.append(20)
# The result is : [0, 2, 4, 6, 8, 20]
print(a)
# + Operator operation 
# Not really adding elements to the tail , Instead, create a new list object ; Copy the elements of the original list and the elements of the new list into the new list object in turn , This will involve a large number of replication operations , For manipulating a large number of elements,... Is not recommended .
# 2184225837376
# 2184225745152
# The address has changed , That is, new objects are created 
print(id(a))
a = a+[10]
print(id(a))
# extend() Method 
# Add all elements of the target list to the end of this list , It belongs to in-situ operation , Do not create new list objects 
# 1368885487872
# 1368885487872
# The address has not changed 
print(id(a))
a.extend([10])
print(id(a))
# insert() Insert elements 
# Use insert Method can insert the specified element into any specified position in the list . This will move all the elements behind the insertion position , It will affect the processing speed . When a large number of elements are involved , Avoid using .
# There are other functions like this movement :remove(),pop(),del(), When they delete non tail elements, they will also move the elements behind the operation position 
a.insert(0, 100)
# The result is :[100, 0, 2, 4, 6, 8, 20, 10, 10]
print(a)
# Multiplication extension 
# Use multiplication to expand the list , Generate a new list , Multiple repetitions of the original list element when a new list element 
print(a*3)
# Deletion of list elements 
# del Delete 
a = [10, 10, 20]
print(a)
del a[2]
print(a)
# pop() Method 
# pop() Delete and return the specified location element , If no location is specified, the last element of the default action list 
a = [10, 20]
a.pop(0)
print(a)
# remove() Method 
# Delete the first occurrence of the specified element , If the element does not exist, an exception is thrown 
a = [10, 20, 30, 20]
a.remove(20)
# The result is :[10, 30, 20]
print(a)
# Access and counting of list elements 
# Direct access to elements through indexes 
a = [10, 20, 10, 30]
print(a[2])
# index() Gets the index of the first occurrence of the specified element in the list 
print(a.index(10))
# from a[1] Start looking for 
print(a.index(10, 1))
# count() Gets the number of times the specified element appears in the list 
# The result of printing is 2
print(a.count(10))
# len() Returns the length of the list 
# Print the results :4
print(len(a))
# Membership judgment 
# Determine whether the specified element exists in the list , We can use count() Method , return 0 It means that there is no , Return is greater than the 0 There is . however , We usually use more concise in Keyword to judge , Go straight back to true or false
print(220 in a)
# Slicing operation 
# The slice is Python Sequence and its important operations , Applicable to list , Tuples , Strings and so on 
# The head is not the tail 
a = [10, 20, 30, 40, 50]
# The result is :[20, 30]
print(a[1:3:1])
# The result of printing is : [40, 50]
print(a[-2:])
# The result of printing is :[50, 40, 30, 20, 10]
print(a[::-1])
# Traversal of list 
for obj in a:
print(obj)
# Sort the list 
# Modify the original list , Do not create a new sort of list 
a = [20, 10, 30]
a.sort()
# Print the results :[10, 20, 30]
print(a)
a.sort(reverse=True)
# Descending The result is :[30, 20, 10]
print(a)
import random
# Out of order 
random.shuffle(a)
print(a)
# Create a new sort of list 
# We can also use built-in object functions sorted() Sort , This method returns a new list , Do not modify the original list 
a = [20, 30, 10, 50]
# Default ascending order 
a = sorted(a)
print(a)
a = sorted(a, reverse=True)
print(a)
# reversed() Return iterator 
# Built in functions reversed() Reverse sorting is also supported , With list objects reverse() The difference is ,
# Built in functions reversed() Don't make any changes to the original list , Just return an iterator object sorted in reverse order 
a = [20, 10, 30]
b = reversed(a)
print(b)
print(list(b))
# Summary of other built-in functions related to the list 
# max and min
# Maximum 
print(max(a))
# minimum value 
print(min(a))
# and If there are non figures, an error will be reported 
print(sum(a))

Today's sharing is here !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved