Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
The 21st official Python column, stop! Don’t miss this zero-based article!
Today we are going to talk about the string type. We have written two articles about the 15,000-word basics and covered a lot of the basics, but strings need to be covered more.
The first two are in the same column. So let’s start.
To recap: Strings versus long strings
Python is very simple and does not have a char (Character) type (familiar to anyone who has done C/Java).
In Python, all single/double/triple quotation marks are strings!
Let’s see what a string looks like
#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # Text1 = "Keep learning keep developing, I'm Ray" Text2 = 'Keep learning keep developing, I'm Ray' Text1 == Text2 # They're the same in Python! Print ("1char substring: %s and type %s "%(text1[0], type(text1[0]))) # We can see that python treats it as a string even though it is a character, because there is no string type in Python! Print (text1[0:4]) print(text1[:]) print(text1[:4]) print(text1[:4]) Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection! """ print(longtext)Copy the code
Readers can directly copy the running code, the committee has added the running renderings:
Special characters: How do I print quotes/newlines in strings?
As mentioned earlier, every programming language has reserved keywords (e.g. ‘break’, ‘continue’, ‘for’, etc.).
Some characters in a string are not printed directly, and in a string processing system they have special characteristics, such as single quotes inside single quoted text. Like how strings hold newlines.
This is why the concept of escape characters has emerged in many languages. Usually the following
\ followed by a character such as \n, \\, \'Copy the code
Below, the committee has prepared some code to show escape characters, from high to low frequencies:
#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # Print ("\'=[']") print("\'=[']") print("\'=[']") Print ("\"=["]") # escape double quotes. Don't have to escape the print in the three quotes long strings (" \ n = [\ n] ") # output print a newline (" =] [\ r \ r ") line # move the cursor to the beginning, So this line of the output is only '] 'print (= "\ [\]") # escape output' symbol print (" \ t = [\ t] ") # transverse tabs, output the Tab key to the same effect, Print ("\b=[\b]") # move the cursor one bit # print("\v=[\v]") Print ("*" * 16)Copy the code
The running effect is as follows:
Of course, there are other escape characters that I don’t usually use and don’t want to mention, hahaha
The next article will cover a string operation.
By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column
Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!