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 :

 Screenshot  2021-10-31  In the morning 11.52.57.png

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 :

 Screenshot  2021-10-31  In the morning 11.58.39.png

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

Random recommended