Home >Backend Development >Python Tutorial >Python's definition and usage of strings and subscripts (examples and analysis included)

Python's definition and usage of strings and subscripts (examples and analysis included)

Tomorin
TomorinOriginal
2018-08-11 18:14:319095browse

Code is composed of a string of letters; String is a word composed of letters that is understood as a word in English but is a variable in Python.

It’s like a point moving to form a line, and a line moving to form a surface. Letters are combined into words, and words are simply encapsulated to turn them into strings. In loop statements, many times we have to locate and intercept multiple strings in a variable.

This article simply introduces to classmates how to intercept letters from a string, or intercept several strings from a group of strings.

First, let’s define:

A string is a string of characters composed of numbers, letters, and underscores.

Generally recorded as

Sth=“a1a2a3a4a5a6a7.........an”(n>0)

Sth is a self-defined independent variable. For novices, we still recommend using English words as variables, which has no harm at all.

There are two types of string values ​​in Python, positive and negative values ​​(see Figure 1.1)

Pythons definition and usage of strings and subscripts (examples and analysis included)

You can use one in It is mathematically wrong, but a very convenient pseudo-definition:

In Python, 0 is a positive integer

In this way, it is easy to understand the expression in the above figure That makes sense

About the use of subscripts and how to intercept strings

#!/usr/bin/python
# -*- coding: UTF-8 -*-                  #国之根基

This is a common convention and cannot be explained

s="Hello world"

This is the string we set ourselves

print s                        #这是对变量s整体进行输出
print s[2:7]                #这是对变量s的第二个下标(第三个字母)到第7个下标()第八个字母进行截取输出
print s[2:]                           #这是对变量s的第二个下标(第三个字母)开始输出到结尾
print s * 2                          #*表示数学中的乘号,s*2表示对变量s输出两次
print s + "There are strings what  add in the s"     #+表示数学中的加号,可以连接两个字符串/数字

This is the interception we made in it

The following is the output Result

Hello world

llo w

llo world

Hello worldHello world

Hello worldThere are strings what add in the s

The above is the detailed content of Python's definition and usage of strings and subscripts (examples and analysis included). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn