Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the common sense of string in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the common sense of string in Python, which is very detailed and has a certain reference value. Friends who are interested must finish reading it!

String and long string

Python is very simple and does not specifically classify a char (Character) type (familiar to students who have done C/Java).

In Python, the single quotation marks / double quotation marks / triple quotation marks are all strings!

Let's see what the string looks like.

#! / usr/bin/env python#-*-coding: utf-8-*-text1 = "continuous learning and continuous development, I am the Lei Xue Committee" text2 = 'continuous Learning and continuous Development, I am the Lei Xue Committee' assert text1 = = text2 # they are the same in python! Print ("1char substring:% s and type% s"% (text1 [0], type (Text1 [0])) # We see that even though it is a character, python treats it as a string because there is no string type in python! Print (text1 [0:4]) print (Text1 [4:]) print (text1 [: 4]) longtext = ""

Readers can copy the running code directly, and the academic committee added the running effect diagram:

Escape characters, such as how to output quotation marks / newlines in a string?

What is an escape character?

For example, some forums contain keyword / sensitive word scanning

We will use pinyin or some homophones to express the same meaning.

The student committee thinks that this is the essence of escaped characters.

That is, another form to express the same meaning, to avoid the limitations of the platform!

As mentioned earlier, every programming language has reserved keywords (such as' break','continue','for', etc.).

Some characters in the string will not be printed directly, in the string processing system, they have particularity, such as single quotation marks appear in the single quotation mark text. For example, how a string saves line breaks.

So the concept of escape characters appears in many languages. It is usually as follows

\ followed by a character such as:\ n,\'

The following school committee has prepared some codes to show escape characters, from high frequency to low frequency:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:13 on 2021-10-30 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Leixue Committee # @ XueWeiTag: CodingDemo# @ File: string_demo2.py# @ Project: hello# commonly used escape character print ("*" * 16) print ("the following is the escape character used from high frequency to low frequency." ") print ("\'= ['] ") # escaped output single quotation marks It is shown here that it is not necessary in a string expanded by double quotes! There is no need to escape print ("\" = ["]") # escape output double quotation marks in a long string of triple quotes, which is shown here to be unnecessary in a string expanded by single quotes! It is not necessary to escape print ("\ n = [\ n]") # newline output print ("\ r = [\ r]") # move the cursor to the beginning of the line in the triple quotation mark long string, so this line output is only'] 'print ("\ = [\]") # escaped output' 'symbol print ("\ t = [\ t]") # horizontal tab, the output Tab key has the same effect Generally, there are 4 spaces (you can type Tab in the blank line of PyCharm to see how many spaces have been skipped) print ("\ b = [\ b]") # cursor moves forward one bit # print ("\ v = [\ v]") # vertical tabs, which the academic committee chooses to ignore. This tab is not supported in Java and belongs to the relatively rare type print ("*" * 16).

The running effect is as follows:

The above is all the content of the article "what is the common sense of strings in Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report