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

How to refer to the string of Python3

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

Share

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

This article mainly explains "how to quote the string of Python3". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to quote the string of Python3".

Coding

By default, Python 3 source files are encoded in UTF-8, and all strings are unicode strings. Of course, you can also specify different encodings for the source files:

# *-coding: cp-1252*- identifier

The first character must be a letter or underscore'_'in the alphabet.

The rest of the identifier consists of letters, numbers, and underscores.

Identifiers are case sensitive.

In Python 3, non-ASCII-encoded identifiers are also allowed.

Python reserved word

Reserved words are keywords, and we cannot use them as any identifier name. Python's standard library provides a keyword module that outputs all reserved words for the current version:

> import keyword > keyword.kwlist ['False',' None', 'True',' _ peg_parser__', 'and',' as', 'assert',' async', 'await',' break', 'class',' continue', 'def',' del', 'elif',' else', 'except',' finally', 'for',' from', 'global',' if', 'import',' in' 'is', 'lambda',' nonlocal', 'not',' or', 'pass',' raise', 'return',' try', 'while',' with', 'yield']

Annotation

In Python, single-line comments begin with #, and multiline comments are enclosed in three pairs of single quotation marks ('') or three pairs of double quotation marks ("").

Indent

The most distinctive feature of Python is the use of indentation to represent blocks of code. The number of indented spaces is variable, but statements of the same code block must contain the same number of indented spaces.

Standard data type

There are six standard data types in Python:

Number (digital)

String (string)

List (list)

Tuple (tuple)

Set (collection)

Dictionary (dictionary)

Of the six standard data types of Python3:

Immutable data (3): Number (number), String (string), Tuple (tuple)

Variable data (3): List (list), Dictionary (dictionary), Set (collection).

String

Single and double quotation marks are used exactly the same in Python.

Use three pairs of quotation marks (''or'') to enclose a multiline string.

Escape character''

A natural string, by adding r or R to the string. For example, r "this is a line with" will show that it is not a line break.

Python allows you to work with unicode strings, prefixed with u or U, such as u "this is an unicode string".

Strings are immutable.

Literally concatenating strings, such as "this", "is" and "string" are automatically converted to this is string.

Strings can be concatenated with the + operator and repeated with the * operator.

Strings in Python can be indexed in two ways, starting with 0 from left to right and 1 from right to left.

Strings in Python cannot be changed.

Python does not have a separate character type, and a character is a string of length 1.

The syntax format for intercepting a string is as follows: variable [header subscript: tail subscript: step size]

Word = 'string' sentence = "this is a sentence." paragraph = "" this is a paragraph and can be made up of multiple lines "".

Example:

#! / usr/bin/python3 str='W3Cschool' print (str) # output string print (str[ 0:-1]) # output all characters from the first to the last print (str [0]) # output string first character print (str [2:5]) # output from the third to the fifth Characters print (str2:]) # output all characters print (str1: 5:2] after the third start # output from the second to the fifth and every two characters print (str * 2) # output string print (str + 'Hello') # connection string print ('- --') print ('helloW3Cschool') # uses the backslash () + n to escape the special character print (ringing helloW3C) # and adds an r to the string Represents the original string and does not escape

R here refers to raw, that is, raw string, which automatically escapes the backslash, for example:

> print ('') # output blank line > print (ringing') # output >

The output result of the above example:

Hello, W3CschoolW3CschooWCscCschool3sW3CschoolW3CschoolW3Cschool-- helloW3CschoolhelloW3Cschool blank line

Functions or methods of a class are separated by blank lines to indicate the beginning of a new piece of code. The class and the function entry are also separated by a blank line to highlight the beginning of the function entry.

Blank lines, unlike code indentation, are not part of the Python syntax. No blank lines are inserted when writing, and the Python interpreter runs without error. However, the function of blank lines is to separate two pieces of code with different functions or meanings, which is convenient for future code maintenance or refactoring.

Remember: blank lines are also part of the program code.

Waiting for user input

Execute the following program to wait for user input after pressing the enter key:

Example:

#! / usr/bin/python3 input ("Press the enter key and exit.")

In the above code, "" outputs two new blank lines before the result is output. Once the user presses the enter key, the program exits.

Display multiple statements on the same line

Python can use multiple statements on the same line, separated by semicolons (;). Here is a simple example:

Example:

#! / usr/bin/python3 import sys; x = 'W3C subscription; sys.stdout.write (x +')

Execute the above code using a script, and the output is as follows:

W3Cschool

Execute using the interactive command line, and the output is:

>

The 10 here represents the number of characters.

Multiple statements form a code group

Indent the same set of statements to form a code block, which we call a code group.

In compound statements such as if, while, def, and class, the first line begins with a keyword and ends with a colon (:), and one or more lines of code after that line form a code group.

We call the first line and the following code group a clause (clause).

The following is an example:

If expression: suiteelif expression: suite else: suiteprint output

The default output of print is newline. If you want to achieve no line wrapping, you need to add end= "" at the end of the variable:

Example:

#! / usr/bin/python3 x = "a" y = "b" # Line feed output print (x) print (y) print ('-') # No line wrap output print (x, end= "") print (y, end= "") print ()

The execution result of the above example is:

Ab-a b

Import and from...import

Import the corresponding module in Python using import or from...import.

Import the entire module (somemodule) in the format: import somemodule

Import a function from a module in the format: from somemodule import somefunction

Import multiple functions from a module in the format: from somemodule import firstfunc, secondfunc, thirdfunc

Import all functions in a module in the format: from somemodule import *

Import sys module import sysprint ('= Python import mode==') print ('command line parameter is:') for i in sys.argv: print (I) print ('python path is', sys.path) Import argv,path member of sys module from sys import argv,path # Import specific member print ('= python from import==') print ('path:',path) # because path members have been imported, no sys.path command line argument is required for reference here

Many programs can perform some operations to view some basic information, and Python can use the h parameter to view the parameter help information:

$pythonhusage: python [option]... [- c cmd | m mod | file |] [arg]... Options and arguments (and corresponding environment variables):-c cmd: program passed in as string (terminates option list)-d: debug output from parser (also PYTHONDEBUG=x)-E: ignore environment variables (such as PYTHONPATH)-h: print this help message and exit [etc.] Thank you for reading. This is the content of "how to quote Python3 strings". After the study of this article, I believe you have a deeper understanding of how to reference the string of Python3, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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