Μορφοποίηση συμβολοσειρών#
Εισαγωγή#
The recommended way to format text and numbers in Python is with f-strings. They allow variables, expressions, and function calls to be embedded directly inside {}.
To create an f-string, add f before the string, and place any variable, expression, or function call inside {}.
x = 1
y = 5
# Display the variables using an f-string
controller.screen.print(f"Position: ({x}, {y})")
# Display a calculation with an f-string
controller.screen.print(f"Sum: {5 + 3}")
# Display the robot's battery capacity with an f-string
controller.screen.print(f"Battery: {controller.get_battery_level()}%")
f-strings – Ενσωματώστε μεταβλητές και εκφράσεις απευθείας στο κείμενο.
f”{value}”– Displays variables, expressions, or function calls inside a string.
Μορφοποίηση αριθμών σε f-strings – Ελέγξτε τον τρόπο εμφάνισης των αριθμητικών τιμών.
:.xf– Sets the number of decimal places to show.round– Rounds a number to a given number of decimal places.:,– Adds commas as thousands separators.:.x%– Converts a decimal to a percentage with x decimal places.:#x– Formats a number as hexadecimal.:b– Formats a number as binary.
Συνδυασμός συμβολοσειρών – Συνδυάστε κείμενο και τιμές.
f”{value}”– Combine strings and variables in a single expression.+operator – Concatenate strings manually with optional type conversion.
Μέθοδοι συμβολοσειράς – Αλλαγή πεζών-κεφαλαίων κειμένου.
Έλεγχοι υποσυμβολοσειράς – Έλεγχος για την παρουσία ή τη θέση κειμένου.
in– Checks if a word exists in a string.startswith– Checks if a string begins with a given value.endswith– Checks if a string ends with a given value.
Ακολουθίες διαφυγής – Μορφοποίηση εξόδου με ειδικούς χαρακτήρες.
Μορφοποίηση αριθμών σε f-strings#
Οι συμβολοσειρές f επιτρέπουν τον ακριβή έλεγχο των δεκαδικών ψηφίων, της στρογγυλοποίησης, των διαχωριστικών χιλιάδων και άλλων, χρησιμοποιώντας τους ακόλουθους προσδιοριστές μορφοποίησης:
Fixed Decimal Places#
.xf controls how many decimal places a number is displayed with.
Usage:
.xf
Παράμετροι |
Περιγραφή |
|---|---|
|
Το πλήθος των δεκαδικών ψηφίων που θα εμφανίζονται. |
# Display pi with 2 decimal places
pi = 3.1415926535
controller.screen.print(f"Pi: {pi:.2f}")
# Output: Pi: 3.14
Rounding Numbers#
round rounds numbers outside of an f-string or inside of the {}.
Usage:
round(number, x)
Παράμετροι |
Περιγραφή |
|---|---|
|
Ο αριθμός που θα στρογγυλοποιηθεί. |
|
Το πλήθος των δεκαδικών ψηφίων για στρογγυλοποίηση. |
# Display a value rounded to only 2 decimal places
value = 5.6789
controller.screen.print(f"{round(value, 2)}")
# Output: 5.68
Thousands Separator#
, inserts commas as thousands separators to make large numbers more readable.
Usage:
,
Παράμετροι |
Περιγραφή |
|---|---|
Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους. |
# Display a large number separated with commas
number = 1234567
controller.screen.print(f"{number:,}")
# Output: 1,234,567
Percentage#
.x% formats decimal values as percentages.
Usage:
.x%
Παράμετροι |
Περιγραφή |
|---|---|
|
Το πλήθος των δεκαδικών ψηφίων που θα εμφανίζονται. |
# Display a converted decimal to a percentage
value = 0.875
controller.screen.print(f"{value:.1%}")
# Output: 87.5%
Hexadecimal#
.#x converts numbers to hexadecimal.
Usage:
.#x
Παράμετροι |
Περιγραφή |
|---|---|
Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους. |
# Convert 255 to hexadecimal
number = 255
controller.screen.print(f"{number:#x}")
# Output: 0xff
Binary#
b converts numbers to binary (base 2).
Usage:
b
Παράμετροι |
Περιγραφή |
|---|---|
Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους. |
# Convert 3 to binary
controller.screen.print(f"Binary: {3:b}")
# Output: Binary: 11
Συνδυασμός χορδών#
Μπορείτε να συνδυάσετε (ή να συνενώσετε) συμβολοσειρές χρησιμοποιώντας δύο προσεγγίσεις:
Using f-strings#
With f-strings, you can embed variables directly inside {}.
# Display an answer based on the given emotion
emotion = "good"
controller.screen.print(f"I'm {emotion}, you?")
+ Operator#
You can combine strings manually using the + operator.
Note: Non-strings must first be converted to strings using str().
# Display the x and y values
x = 10
y = 20
controller.screen.print("X: " + str(x) + ", Y: " + str(y))
Μέθοδοι συμβολοσειρών#
Η Python παρέχει ενσωματωμένες μεθόδους για την τροποποίηση και τον έλεγχο συμβολοσειρών.
upper#
upper converts all letters in a string to uppercase.
Usage:
upper()
Παράμετροι |
Περιγραφή |
|---|---|
Αυτή η μέθοδος δεν έχει παραμέτρους. |
# Display a message in uppercase
message = "vexcode"
controller.screen.print(message.upper())
# Output: VEXCODE
lower#
lower converts all letters in a string to lowercase.
Usage:
lower()
Παράμετροι |
Περιγραφή |
|---|---|
Αυτή η μέθοδος δεν έχει παραμέτρους. |
# Display a message in lowercase
message = "VEXCODE"
controller.screen.print(message.lower())
# Output: vexcode
Έλεγχος για υποσυμβολοσειρές#
in#
in is a keyword that returns a Boolean indicating whether a substring (word or piece of text) exists in a string.
True– The word exists in the string.False– The word does not exist in the string.
# Display a message if "Hey" is in the string
message = "Hey everyone!"
if "Hey" in message:
controller.screen.print("Hello!")
startswith#
startswith returns a Boolean indicating whether a string begins with a given value.
True– The word starts the string.False– The word does not start the string.
Usage:
startswith(substring)
Παράμετροι |
Περιγραφή |
|---|---|
|
Η δευτερεύουσα συμβολοσειρά που θα ελεγχθεί στην αρχή της συμβολοσειράς. |
# Display a message if the string starts with "AIR"
message = "AIR Drone"
if message.startswith("AIR"):
controller.screen.print("AIR first!")
endswith#
endswith returns a Boolean indicating whether a string ends with a given value.
True– The word ends the string.False– The word does not end the string.
Usage:
endswith(substring)
Παράμετροι |
Περιγραφή |
|---|---|
|
Η δευτερεύουσα συμβολοσειρά που θα ελεγχθεί στο τέλος της συμβολοσειράς. |
# Display a message if the string ends with "Drone"
message = "AIR Drone"
if message.endswith("Drone"):
controller.screen.print("Drone last!")
Ακολουθίες διαφυγής#
Οι ακολουθίες διαφυγής είναι ειδικοί χαρακτήρες που χρησιμοποιούνται μέσα σε συμβολοσειρές για τη μορφοποίηση του κειμένου που εξάγεται. Είναι διαθέσιμοι μόνο για χρήση με την Κονσόλα.
New Line#
\n moves text to a new line when printing.
# Display text on two lines
print("First line\nSecond line")
Tab Spacing#
\t inserts a tab space between words or numbers.
# Display the quantity of barrels
quantity = 2
print("IDs:\t", quantity)