Μορφοποίηση συμβολοσειρών#

Εισαγωγή#

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().

def main():
    x = 1
    y = 5

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

# Start threads — Do not delete
start_thread(main)

def main():
    # Display a calculation using .format()
    console.print("Sum: {}".format(5 + 3))

# Start threads — Do not delete
start_thread(main)

def main():
    wait(1, SECONDS)
    # Display the time using .format()
    console.print("Time: {}".format(timer.time(SECONDS)))

# Start threads — Do not delete
start_thread(main)

Formatting Numbers with .format – Insert values and expressions into a string.

  • :.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.

Συνδυασμός συμβολοσειρών – Συνδυάστε κείμενο και τιμές.

  • .format() – Combine strings and variables in a single expression.

  • + operator – Concatenate strings manually with optional type conversion.

Μέθοδοι συμβολοσειράς – Αλλαγή πεζών-κεφαλαίων κειμένου.

  • upper() – Converts all characters to uppercase.

  • lower() – Converts all characters to lowercase.

Έλεγχος για υποσυμβολοσειρές – Έλεγχος για την παρουσία ή τη θέση του κειμένου.

  • 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.

Ακολουθίες διαφυγής – Μορφοποίηση εξόδου με ειδικούς χαρακτήρες.

  • \n – Adds a line break (new line).

  • \t – Adds a tab space between items.

Formatting Numbers with .format#

.format can be used to control how numbers appear, including decimal places, rounding, and other formatting styles such as:

Fixed Decimal Places#

:.xf controls how many decimal places a number is displayed with.

Usage:
:.xf

Παράμετροι

Περιγραφή

x

Το πλήθος των δεκαδικών ψηφίων που θα εμφανίζονται.

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

# Start threads — Do not delete
start_thread(main)

Rounding Numbers#

round rounds a number to a specific number of decimal places before formatting.

Usage:
round(number, x)

Παράμετροι

Περιγραφή

number

Ο αριθμός που θα στρογγυλοποιηθεί.

x

Το πλήθος των δεκαδικών ψηφίων για στρογγυλοποίηση.

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

# Start threads — Do not delete
start_thread(main)

Thousands Separator#

:, inserts commas as thousands separators to make large numbers more readable.

Usage:
:,

Παράμετροι

Περιγραφή

Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.

def main():
    # Display a large number separated with commas
    number = 1234567
    console.print("Formatted: ")
    console.new_line()
    console.print("{:,}".format(number))
    # Output: Formatted: 1,234,567

# Start threads — Do not delete
start_thread(main)

Percentage#

:.x% formats decimal values as percentages by multiplying the value by 100 and adding a percent sign.

Usage:
:.x%

Παράμετροι

Περιγραφή

x

Το πλήθος των δεκαδικών ψηφίων που θα εμφανίζονται.

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

# Start threads — Do not delete
start_thread(main)

Hexadecimal#

:#x converts numbers to hexadecimal.

Usage:
:#x

Παράμετροι

Περιγραφή

Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.

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

# Start threads — Do not delete
start_thread(main)

Binary#

:b converts numbers to binary (base 2).

Usage:
:b

Παράμετροι

Περιγραφή

Αυτός ο προσδιοριστής μορφής δεν έχει παραμέτρους.

def main():
    # Convert 3 to binary
    console.print("Binary: {:b}".format(3))
    # Output: Binary: 11

# Start threads — Do not delete
start_thread(main)

Συνδυασμός χορδών#

Μπορείτε να συνδυάσετε (ή να συνενώσετε) συμβολοσειρές χρησιμοποιώντας δύο προσεγγίσεις:

Using .format#

Insert values directly into the string using {}.

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

# Start threads — Do not delete
start_thread(main)

+ Operator#

Join multiple parts by using +.

Note: Non-strings must first be converted to strings using str().

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

# Start threads — Do not delete
start_thread(main)

Μέθοδοι συμβολοσειρών#

Η Python παρέχει ενσωματωμένες μεθόδους για την τροποποίηση και τον έλεγχο συμβολοσειρών.

upper#

upper converts all letters in a string to uppercase.

Usage:
upper()

Παράμετροι

Περιγραφή

Αυτή η μέθοδος δεν έχει παραμέτρους.

def main():
    # Capitalize a string with .upper()
    message = "vexcode"
    console.print(message.upper())  # Output: VEXCODE

# Start threads — Do not delete
start_thread(main)

lower#

lower converts all letters in a string to lowercase.

Usage:
lower()

Παράμετροι

Περιγραφή

Αυτή η μέθοδος δεν έχει παραμέτρους.

def main():
    # Make a string lowercase with .lower()
    message = "VEXCODE"
    console.print(message.lower())  # Output: vexcode

# Start threads — Do not delete
start_thread(main)

Έλεγχος για υποσυμβολοσειρές#

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.

def main():
    message = "Hey everyone!"
    if "Hey" in message:
        console.print("Hello!")

# Start threads — Do not delete
start_thread(main)

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)

Παράμετροι

Περιγραφή

substring

Η δευτερεύουσα συμβολοσειρά που θα ελεγχθεί στην αρχή της συμβολοσειράς.

def main():
    # Check for 'GO' at the start of a string
    message = "GO Robot"

    if message.startswith("GO"):
        console.print("GO first!")

# Start threads — Do not delete
start_thread(main)

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)

Παράμετροι

Περιγραφή

substring

Η δευτερεύουσα συμβολοσειρά που θα ελεγχθεί στο τέλος της συμβολοσειράς.

def main():
    # Check for `Robot` at the end of a string
    message = "GO Robot"

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

# Start threads — Do not delete
start_thread(main)

Ακολουθίες διαφυγής#

Οι ακολουθίες διαφυγής είναι ειδικοί χαρακτήρες που χρησιμοποιούνται μέσα σε συμβολοσειρές για τη μορφοποίηση του κειμένου εξόδου.

New Line#

\n moves text to a new line when printing.

def main():
    # Display text on two lines
    console.print("First line\nSecond line")

# Start threads — Do not delete
start_thread(main)

Tab Spacing#

\t inserts a tab space between words or numbers.

def main():
    # Display the quantity of disks
    quantity = 2
    console.print("Disks:\t", quantity)

# Start threads — Do not delete
start_thread(main)