current position:Home>String and related methods of Python data type introduction
String and related methods of Python data type introduction
2022-01-29 21:17:52 【Mr. forgets his name】
Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .
1、python String type of , keyword str; Definition : Pairs of single and double quotation marks enclose the string
Be careful :python Define variables in , Do not need to use variable type keywords :str character string int integer float floating-point tuple Tuples dict Dictionaries list list
str_2="lemon python class "
str_1='PythonAotuTest'
print(type(str_1))
Copy code
2、 The value of the string , He is orderly , A string is composed of elements one by one , And there's an index index, from 0 Angle mark start number , It also supports reverse order from -1 Start
print(" Take the value of string variable :",str_2)
print(" Positive values :",str_2[2]," Reverse value :",str_2[-16])
Copy code
3、 Special characters \n \t \r, escape Add... Before the string r/R Or backslash \, After the transfer is the ordinary string output \n
str_2=r"python\nclass"
print(str_2)
Copy code
4、 String operations + Splicing ,* ride . priority ; Support the same type + Splicing : Different types of splicing , Need to transform , Compulsive . That is, the original type is forced to be spliced with the same type
# print(str_2+str_1*4)
# i=(1,2,3)
# j=111
# print(str(j)+str_1)
# print(str(i)+str_1)
Copy code
5、 member operator :in not in
print(" member operator , conditional :",'i' not in str_2,'o' not in str_2)
Copy code
6、 Slice value m:n:k The starting position : Target location : step ( Isochromatic ) The default is 1, Take left instead of right n-1 End value
# [:] Take it from head to tail ,[2:] from 2 Start taking to the end [::2] from 0 Start , The isochron is 2 Take to the tail ,[:7:] from 0 Fetch 7 The isochron is 1
print(" Slice value :",str_2[1:16:3],", Support reverse order slicing :",str_2[-16:-3:1]) # The reverse order is [-16:] Tolerable
# print(str_2[::2])
Copy code
7、 Format output ,%s character string ( You can put any type of value ) %d Integers %f Floating point numbers ,%.2f Keep two decimal places
# %() Coercive transformation , commas , The order value is
# format {} The space occupation can not mark the position , The default order value is ,index from 0 Start , If the index value , Just mark by index
age=19
name='python'
print(" Format output :{1}{0}".format(age,name),", Place holder :%s Output %s:"%(str_2,age))
Copy code
8、 Use of built-in functions
# Case Toggle :upper()、lower()
print(" Capitalization :",str_2.upper()," A lowercase letter :",str_1.lower())
# swapcase() Capitalize interchangeably
print(str_1.swapcase())
# find() Look for the element
print(" Find the index of the element index:",str_2.find("t"),", Element index not found index Default return :",str_1.find("3"))
# Substitution function replace(), Be replaced , Replacement value , Specify how many times to replace
print(str_1," Comparison before and after string element replacement :",str_1.replace("t","T",2))
# split() function , Cut string , Return list type , The type of list element is still string
str_4=str_2.split("n")
print(str_2," Comparison of string elements before and after cutting :",str_2.split("n"),", Type after cutting :",type(str_4),
", But the elements inside \'{}\' Type or string :".format(str_4[2]),type(str_4[2]))
# strip() Handle the character elements at the beginning and end
print(str_2," Comparison of string elements before and after processing :",str_2.strip("l"))
# Length function __len__()
print(" Return string str_2 The length of :",str_2.__len__())
Copy code
copyright notice
author[Mr. forgets his name],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201292117492108.html
The sidebar is recommended
- Compile D + +, and use d to call C from python
- Install tensorflow and python 3.6 in Windows 7
- Python collects and monitors system data -- psutil
- Python collects and monitors system data -- psutil
- Finally, this Python import guide has been sorted out. Look!
- Quickly build Django blog based on function calculation
- Getting started with Python - object oriented - special methods
- Teach you how to use Python to transform an alien invasion game
- You can easily get started with Excel. Python data analysis package pandas (VI): sorting
- Implementation of top-level design pattern in Python
guess what you like
-
Using linear systems in python with scipy.linalg
-
Python tiktok 5000+ V, and found that everyone love this video.
-
Using linear systems in python with scipy.linalg
-
How to get started quickly? How to learn Python
-
Modifying Python environment with Mac OS security
-
You can easily get started with Excel. Python data analysis package pandas (XI): segment matching
-
Advanced practical case: Javascript confusion of Python anti crawling
-
Better use atom to support jupyter based Python development
-
Better use atom to support jupyter based Python development
-
Fast power modulus Python implementation of large numbers
Random recommended
- Python architects recommend the book "Python programmer's Guide" which must be read by self-study Python architects. You are welcome to take it away
- Decoding the verification code of Taobao slider with Python + selenium, the road of information security
- Python game development, pyGame module, python implementation of skiing games
- This paper clarifies the chaotic switching operation and elegant derivation of Python
- You can easily get started with Excel. Python data analysis package pandas (3): making score bar
- Test Development: self study Dubbo + Python experience summary and sharing
- Python + selenium automated test: page object mode
- You can easily get started with Excel. Python data analysis package pandas (IV): any grouping score bar
- Opencv skills | saving pictures in common formats as transparent background pictures (with Python source code) - teach you to easily make logo
- You can easily get started with Excel. Python data analysis package pandas (V): duplicate value processing
- Python ThreadPoolExecutor restrictions_ work_ Queue size
- Python generates and deploys verification codes with one click (Django)
- With "Python" advanced, you can catch all the advanced syntax! Advanced function + file operation, do not look at regret Series ~
- At the beginning of "Python", you must see the series. 10000 words are only for you. It is recommended to like the collection ~
- [Python kaggle] pandas basic exercises in machine learning series (6)
- Using linear systems in python with scipy.linalg
- The founder of pandas teaches you how to use Python for data analysis (mind mapping)
- Using Python to realize national second-hand housing data capture + map display
- Python image processing, automatic generation of GIF dynamic pictures
- Pandas advanced tutorial: time processing
- How to make Python run faster? Six tips!
- Django: use of elastic search search system
- Fundamentals of Python I
- Python code reading (chapter 35): fully (deeply) expand nested lists
- Python 3.10 official release
- Solution of no Python 3.9 installation was detected when uninstalling Python
- This pandas exercise must be successfully won
- [Python homework] coupling network information dissemination
- Python application software development tool - tkinterdesigner v1.0 5.1 release!
- [Python development tool Tkinter designer]: Lecture 2: introduction to Tkinter designer's example project
- [algorithm learning] sword finger offer 64 Find 1 + 2 +... + n (Java / C / C + + / Python / go / trust)
- leetcode 58. Length of Last Word(python)
- Problems encountered in writing the HTML content of articles into the database during the development of Django blog
- leetcode 1261. Find Elements in a Contaminated Binary Tree(python)
- [algorithm learning] 1486 Array XOR operation (Java / C / C + + / Python / go / trust)
- Understand Python's built-in function and add a print function yourself
- Python implements JS encryption algorithm in thousands of music websites
- leetcode 35. Search Insert Position(python)
- leetcode 1829. Maximum XOR for Each Query(python)
- [introduction to Python visualization]: 12 small examples of complete data visualization, taking you to play with visualization ~