文字列の書式設定#

導入#

The .format() method in Python is used to create strings that include variables, expressions, or values. This method replaces curly brace placeholders {} in a string with values provided inside .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 — 数値を16進数としてフォーマットします。

  • :b — 数値をバイナリ形式にフォーマットします。

色 — コンソール内のテキストの色を変更します

  • Colors — コンソールのテキストの色を設定します。

文字列の結合 — テキストと値を結合します。

  • .format() — 文字列と変数を単一の式に組み合わせます。

  • + operator — オプションの型変換を使用して文字列を手動で連結します。

文字列メソッド — テキストの大文字/小文字を変換する。

  • upper() — すべての文字を大文字に変換します。

  • lower() — すべての文字を小文字に変換します。

部分文字列のチェック — テキストの存在または位置をテストします。

  • in — 文字列内に単語が存在するかどうかを確認します。

  • startswith() — 文字列が指定された値で始まるかどうかを確認します。

  • endswith() — 文字列が指定された値で終わるかどうかを確認します。

エスケープシーケンス — 特殊文字を使用して出力をフォーマットします。

  • \n — 改行を追加します。

  • \t — 項目間にタブスペースを追加します。

数値を .format#

.format 使用すると、小数点以下の桁数、丸め処理、その他の書式設定スタイルなど、数値の表示方法を制御できます。

Fixed Decimal Places#

.xf 、数値の小数点以下の桁数を制御します。

Usage:
.xf

パラメータ

説明

x

表示する小数点以下の桁数。

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

Rounding Numbers#

round は、書式設定を行う前に数値を特定の小数点以下の桁数に丸めます。

Usage:
round(number, x)

パラメータ

説明

number

四捨五入する数値。

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

Thousands Separator#

:, 、大きな数字を読みやすくするために、桁区切りとしてカンマを挿入します。

Usage:
:,

パラメータ

説明

この書式指定子にはパラメータがありません。

# 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

Percentage#

:.x% は、小数値を 100 倍してパーセント記号を追加することにより、パーセント形式に変換します。

Usage:
:.x%

パラメータ

説明

x

表示する小数点以下の桁数。

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

Hexadecimal#

:#x は数値を 16 進数に変換します。

Usage:
:#x

パラメータ

説明

この書式指定子にはパラメータがありません。

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

Binary#

:b 数値をバイナリ (基数 2) に変換します。

Usage:
:b

パラメータ

説明

この書式指定子にはパラメータがありません。

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

色#

印刷コンソールに印刷する際のテキストの色は、以下のカラーコードを使用することで変更できます。

  • [31m — Red

  • [32m — Green

  • [34m — Blue

  • [30m — Black

  • [37m — White

  • [33m — Yellow

  • [91m — Orange

  • [35m — Purple

  • [36m — Cyan

  • [97m — Transparent

すべてのカラーコードは、機能するためにエスケープシーケンス = \033 に従う必要があります。

文字列とともに、 print コマンドでこれを直接使用できます。

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

また、これ単体で使用して、以降のすべての print コマンドを設定した色に設定することもできます。

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

文字列の結合#

文字列を結合(連結)するには、次の2つの方法があります。

Using .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 、文字列内のすべての文字を大文字に変換します。

Usage:
upper()

パラメータ

説明

このメソッドにはパラメータがありません。

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

lower#

lower 、文字列内のすべての文字を小文字に変換します。

Usage:
lower()

パラメータ

説明

このメソッドにはパラメータがありません。

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

部分文字列のチェック#

in#

in は、文字列内に部分文字列(単語またはテキストの一部)が存在するかどうかを示すブール値を返すキーワードです。

  • True — その単語は文字列内に存在します。

  • False — 文字列内にその単語は存在しません。

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

startswith#

startswith は、文字列が指定された値で始まるかどうかを示すブール値を返します。

  • True — その単語が文字列の開始点です。

  • False — その単語は文字列の先頭にありません。

Usage:
startswith(substring)

パラメータ

説明

substring

文字列の先頭でチェックする部分文字列。

message = "IQ Robot"

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

endswith#

endswith は、文字列が指定された値で終わるかどうかを示すブール値を返します。

  • True — その単語は文字列の終わりです。

  • False — その単語は文字列の終わりではありません。

Usage:
endswith(substring)

パラメータ

説明

substring

文字列の末尾でチェックする部分文字列。

message = "IQ Robot"

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

脱出シーン#

エスケープシーケンスは、文字列内でテキスト出力の書式設定に使用される特殊文字です。これらはコンソールでのみ使用できます。

New Line#

\n 、印刷時にテキストを新しい行に移動します。

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

Tab Spacing#

\t は、単語または数字の間にタブスペースを挿入します。

# Display the quantity of blocks
quantity = 2
print("Blocks:\t", quantity)