basic syntax
This document provides an overview of the basic syntax used in our programming language.
变量 Variables
操作符运算 Compute
算术运算 Arithmetic Operations
**: 幂运算 (Exponentiation)//: 整除 (Floor Division)
字符串操作 String Operations
字符串前缀 String Prefixes
r: 原始字符串 (Raw String) >> 用于表示字符串中的反斜杠不作为转义字符。f: 格式化字符串 (Formatted String) >> 用于在字符串中,嵌入变量或表达式的值,即{value}。p: 打印字符串 (Print String) >> 用于输出字符串内容。
字符串方法 String Methods
提示
切片索引有有用的默认值;省略的第一个索引默认为零,省略的第二个索引默认为被切片的字符串的大小。
- 索引 (Indexing) >> 通过索引访问字符串中的单个字符,索引从0开始。索引也可以是负数,表示从字符串末尾开始计数。
- 切片 (Slicing) >> 通过指定起始和结束索引,获取字符串的子串。切片的语法为
string[start:end],其中start是起始索引,end是结束索引(不包含)。
额外的字符串操作 Extra String Operations
+: 字符串连接 (String Concatenation) >> 用于将多个字符串连接在一起。*: 字符串重复 (String Repetition) >> 用于将字符串- 相邻的字符串字面值自动连接 (Adjacent String Literals Concatenation) >> 当两个或多个字符串字面值相邻时,它们会自动连接在一起,无需使用
+运算符。
综合 Examples
# 3 times 'ha', followed by 'Ium'
print(3 * 'ha' + 'Ium')
# Output: 'hahahaIum'
# two adjacent string literals
print('Hello,' ' world!')
# Output: 'Hello, world!'
# so below are equivalent
print('i want to lay down'
'right now')
# Output: 'i want to lay downright now'