current position:Home>Go through the string common sense in Python
Go through the string common sense in Python
2022-01-30 18:18:56 【Lei Xuewei】
Little knowledge , Great challenge ! This article is participating in 「 A programmer must have a little knowledge 」 Creative activities
This article has participated in 「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .
ceremonial Python Column No 21 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !
Today, let's talk about string types , I have written two articles in total 1.5 The foundation of ten thousand words is repeated , It involves a lot of basic knowledge , But the string still needs to be talked about more .
The first two are in the same column of this article , Welcome to your attention . Let's start with .
Take a look back. : String and long string
Python It's simple , There is no special one char(Character) type ( Have done C/Java My classmates are familiar with it )
stay Python in , By single quotes / Double quotes / The three quotation marks are all strings !
Let's see what the string looks like
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 10:13 In the morning
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : string_demo.py
# @Project : hello
text1 = " Continuous learning and continuous development , I'm Lei Xuewei "
text2 = ' Continuous learning and continuous development , I'm Lei Xuewei '
assert text1 == text2 # They are python It's the same inside !
# Substring of string
print("1char substring : %s and type %s "%(text1[0], type(text1[0]))) # We see that although it is a character ,python Also treat it as a string , because python There is no string type in !
print(text1[0:4])
print(text1[4:])
print(text1[:4])
longtext = """
Continuous learning and continuous development , I am a 【 Lei Xuewei 】!
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !
"""
print(longtext)
Copy code
Readers can directly copy the running code , The school committee added the operation effect diagram :
Special characters : How to output quotation marks in a string / Line break ?
As mentioned earlier, every programming language has reserved keywords ( such as ‘break’,‘continue’,‘for’ wait ).
Some characters in the string will not be printed directly , In a string processing system , They are special , For example, single quotation marks appear in single quotation mark text . For example, how to save a string and wrap it .
So the concept of escape character appears in many languages . Usually as follows
\ Followed by a character
such as :\n, \\, \'
Copy code
Next, the school committee has prepared some codes to show the escape characters , From high frequency to low frequency :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 10:13 In the morning
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : string_demo2.py
# @Project : hello
# Commonly used escape characters
print("*" * 16)
print(" The following are the escape symbols used from high frequency to low frequency according to Lei Xuewei ")
print("\'=[']") # Escape output single quotation marks , Here we show that in a string with double quotation marks, you don't have to ! There is no need to escape in a three quote long string
print("\"=["]") # Escape output double quotes , Here we show that in a string with single quotation marks, you don't have to ! There is no need to escape in a three quote long string
print("\n=[\n]") # Line feed output
print("\r=[\r]") # Move the cursor to the beginning of the line , So this line of output is just ']'
print("\=[\]") # Escape output '' Symbol
print("\t=[\t]") # Horizontal tabs , Output Tab Key equivalent effect , commonly 4 A space ( Can be in PyCharm Enter... On a blank line Tab Look, I jumped a few spaces )
print("\b=[\b]") # Move the cursor forward one bit
# print("\v=[\v]") # Vertical tabs , The school committee chose to ignore , This tab is in Java There is no support in , It belongs to a relatively rare type
print("*" * 16)
Copy code
The operation effect is as follows :
Of course, there are other escape characters , I don't often and don't want to mention , Ha ha ha
The next article will introduce the operation of writing string .
by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column
Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !
copyright notice
author[Lei Xuewei],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301818551966.html
The sidebar is recommended
- Exploratory data analysis (EDA) in Python using SQL and Seaborn (SNS).
- Turn audio into shareable video with Python and ffmpeg
- Using rbind in python (equivalent to R)
- Pandas: how to create an empty data frame with column names
- Talk about quantifying investment using Python
- Python, image restoration in opencv - CV2 inpaint
- Python notes (14): advanced technologies such as object-oriented programming
- Python notes (13): operations such as object-oriented programming
- Python notes (12): inheritance such as object-oriented programming
- Chapter 2: Fundamentals of python-5 Boolean
guess what you like
-
Python notes (11): encapsulation such as object-oriented programming
-
Python notes (10): concepts such as object-oriented programming
-
Gradient lifting method and its implementation in Python
-
Van * Python | simple crawling of a site course
-
Chapter 1 preliminary knowledge of pandas (list derivation and conditional assignment, anonymous function and map method, zip object and enumerate method, NP basis)
-
Nanny tutorial! Build VIM into an IDE (Python)
-
Fourier transform of Python OpenCV image processing, lesson 52
-
Introduction to python (III) network request and analysis
-
China Merchants Bank credit card number recognition project (Part I), python OpenCV image processing journey, Part 53
-
Introduction to python (IV) dynamic web page analysis and capture
Random recommended
- Python practice - capture 58 rental information and store it in MySQL database
- leetcode 119. Pascal's Triangle II(python)
- leetcode 31. Next Permutation(python)
- [algorithm learning] 807 Maintain the city skyline (Java / C / C + + / Python / go / trust)
- The rich woman's best friend asked me to write her a Taobao double 11 rush purchase script in Python, which can only be arranged
- Glom module of Python data analysis module (1)
- Python crawler actual combat, requests module, python realizes the full set of skin to capture the glory of the king
- Summarize some common mistakes of novices in Python development
- Python libraries you may not know
- [Python crawler] detailed explanation of selenium from introduction to actual combat [2]
- This is what you should do to quickly create a list in Python
- On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
- Python OpenCV image area contour mark, which can be used to frame various small notes
- How to set up an asgi Django application with Postgres, nginx and uvicorn on Ubuntu 20.04
- Initial Python tuple
- Introduction to Python urllib module
- Advanced Python Basics: from functions to advanced magic methods
- Python Foundation: data structure summary
- Python Basics: from variables to exception handling
- Python notes (22): time module and calendar module
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
- Python notes (22): errors and exceptions
- Python has been hidden for ten years, and once image recognition is heard all over the world
- Python notes (21): random number module
- Python notes (19): anonymous functions
- Use Python and OpenCV to calculate and draw two-dimensional histogram
- Python, Hough circle transformation in opencv
- A library for reading and writing markdown in Python: mdutils
- Datetime of Python time operation (Part I)
- The most useful decorator in the python standard library
- Python iterators and generators