字符串格式化#

介绍#

Python 中的 .format() 方法用于创建包含变量、表达式或值的字符串。此方法将字符串中的大括号占位符 {} 替换为 .format() 中提供的值。

x = 1
y = 5

# Display the variables using .format()
brain.screen.print("Position: ({}, {})".format(x, y))

# Display a calculation using .format()
brain.screen.print("Sum: {}".format(5 + 3))

# Display the robot's battery capacity using .format()
brain.screen.print("Battery: {}%".format(brain.battery.capacity()))

使用 .format 格式化数字 - 将值和表达式插入字符串。

  • :.xf – 设置要显示的小数位数。

  • round – 将数字四舍五入到给定的小数位数。

  • :, – 添加逗号作为千位分隔符。

  • :.x% – 将小数转换为具有 x 位小数的百分比。

  • :#x – 将数字格式化为十六进制。

  • :b – 将数字格式化为二进制。

颜色——更改控制台中文本的颜色

  • Colors – 设置控制台的文本颜色。

组合字符串——组合文本和值。

  • .format() – 将字符串和变量组合在一个表达式中。

  • + 运算符 – 使用可选的类型转换手动连接字符串。

字符串方法——更改文本的大小写。

  • upper() – 将所有字符转换为大写。

  • lower() – 将所有字符转换为小写。

检查子字符串——测试文本的存在或位置。

  • in – 检查字符串中是否存在某个单词。

  • startswith() – 检查字符串是否以给定值开头。

  • endswith() – 检查字符串是否以给定值结尾。

转义序列 – 使用特殊字符格式化输出。

  • \n – 添加换行符(新行)。

  • \t – 在项目之间添加制表符空格。

使用 .format 格式化数字#

.format 可用于控制数字的显示方式,包括小数位、舍入和其他格式样式,例如:

固定小数位#

.xf 控制数字显示的小数位数。

用法:
.xf

参数

描述

x

要显示的小数位数。

# Display pi with 2 decimal places
pi = 3.1415926535
brain.screen.print("Pi: {:.2f}".format(pi))
# Output: Pi: 3.14

四舍五入#

round 在格式化之前将数字四舍五入到特定的小数位数。

用法:
round(number, x)

参数

描述

数字

要舍入的数字。

x

要四舍五入的小数位数。

# Display a value rounded to only 2 decimal places
value = 5.6789
brain.screen.print("Rounded: {}".format(round(value, 2)))
# Output: Rounded: 5.68

千位分隔符#

:, 插入逗号作为千位分隔符,使大数字更易读。

用法:
:,

参数

描述

此格式说明符没有参数。

# Display a large number separated with commas
number = 1234567
brain.screen.print("Formatted: ")
brain.screen.next_row()
brain.screen.print("{:,}".format(number))
# Output: Formatted: 1,234,567

百分比#

:.x% 将小数值格式化为百分比。

用法:
:.x%

参数

描述

x

要显示的小数位数。

# Display a converted decimal to a percentage
value = 0.875
brain.screen.print("Score: {:.1%}".format(value))
# Output: Score: 87.5%

十六进制#

:#x 将数字转换为十六进制。

用法:
:#x

参数

描述

此格式说明符没有参数。

# Convert 255 to hexadecimal
number = 255
brain.screen.print("Hex: {:#x}".format(number))
# Output: Hex: 0xff

二进制#

:b 将数字转换为二进制(以 2 为基数)。

用法:
:b

参数

描述

此格式说明符没有参数。

# Convert 3 to binary
brain.screen.print("Binary: {:b}".format(3))
# Output: Binary: 11

颜色#

您可以使用以下颜色代码更改打印到打印控制台时文本的颜色:

  • [31米 – 红色

  • [32米 – 格林

  • [34米 – 蓝色

  • [30m – 黑色

  • [37米 – 白色

  • [33米 – 黄色

  • [91米 – 橙色

  • [35米 – 紫色

  • [36m – 青色

  • [97m – 透明

所有颜色代码都需要遵循转义序列 = \033 才能起作用。

您可以直接在带有字符串的“print”命令中使用它。

# Print VEXcode in Red to the Print Console
print("\033[31mVEXcode")

您还可以单独使用它来将所有“打印”命令设置为设置的颜色。

# Set the Print Console text color to Red before printing
print("\033[31m")
print("VEXcode")

组合字符串#

您可以使用两种方法来组合(或连接)字符串:

使用 .format#

使用“{}”将值直接插入字符串。

# Display an answer based on the given emotion
emotion = "good"
brain.screen.print("I'm {}, you?".format(emotion))

+ Operator#

使用‘+’连接多个部分。

**注意:**必须首先使用 str() 将非字符串转换为字符串。

# Display the x and y values
x = 10
y = 20
brain.screen.print("X: " + str(x) + ", Y: " + str(y))

字符串方法#

Python 提供了修改和检查字符串的内置方法。

#

“upper”将字符串中的所有字母转换为大写。

用法:
upper()

参数

描述

该方法没有参数。

message = "vexcode"
brain.screen.print(message.upper())  # Output: VEXCODE

降低#

“lower”将字符串中的所有字母转换为小写。

用法:
lower()

参数

描述

该方法没有参数。

message = "VEXCODE"
brain.screen.print(message.lower())  # Output: vexcode

检查子字符串#

#

in 是一个关键字,它返回一个布尔值,指示字符串中是否存在某个单词。

  • True - 该单词存在于字符串中。

  • False - 该单词在字符串中不存在。

message = "Hey everyone!"
if "Hey" in message:
    brain.screen.print("Hello!")

始于#

startswith 返回一个布尔值,指示字符串是否以给定值开头。

  • True - 该单词开始字符串。

  • False - 该词不作为字符串的开头。

用法:
startswith(substring)

参数

描述

子串

要在字符串内检查的子字符串。

message = "AIM Robot"

if message.startswith("AIM"):
    brain.screen.print("AIM first!")

以…结尾#

endswith 返回一个布尔值,指示字符串是否以给定值结尾。

  • True——该词结束字符串。

  • False - 该词不会结束字符串。

用法:
startswith(substring)

参数

描述

子串

要在字符串内检查的子字符串。

message = "AIM Robot"

if message.endswith("Robot"):
    brain.screen.print("Robot last!")

转义序列#

转义序列是字符串中用于格式化文本输出的特殊字符。它们适用于控制台。

新线#

打印时,‘\n‘将文本移动到新行。

# Display text on two lines
print("First line\nSecond line")

制表符间距#

\t 在单词或数字之间插入一个制表符空格,

# Display the quantity of barrels
quantity = 2
print("Barrels:\t", quantity)