Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

2.4 Triple-Quoted Strings στην Python

Τα triple-quoted strings στην Python είναι ένας ειδικός τύπος string που αρχίζουν και τελειώνουν είτε με τρία διπλά εισαγωγικά (“””) είτε με τρία μονά εισαγωγικά (”’). Συνιστάται η χρήση τριών διπλών εισαγωγικών (“””) βάσει του Οδηγού Στυλ για την Python Σύνταξη. Χρησιμοποιούνται για τη δημιουργία:

  1. Πολυγραμμικών strings (multiline strings),
  2. Strings που περιέχουν μονά ή διπλά εισαγωγικά,
  3. Docstrings, οι οποίοι αποτελούν τον συνιστώμενο τρόπο για να τεκμηριώσετε τους σκοπούς ορισμένων συστατικών ενός προγράμματος.

Παράδειγμα πολυγραμμικού string:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
multiline_string = """This is an example
of a multiline string,
which spans several lines."""
print(multiline_string)
multiline_string = """This is an example of a multiline string, which spans several lines.""" print(multiline_string)
multiline_string = """This is an example
of a multiline string,
which spans several lines."""
print(multiline_string)

Αποτέλεσμα εκτέλεσης στην οθόνη:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This is an example
of a multiline string,
which spans several lines.
This is an example of a multiline string, which spans several lines.
This is an example
of a multiline string,
which spans several lines.

Παράδειγμα string που περιέχει και μονά και διπλά εισαγωγικά:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
quote_string = """This string includes both 'single' and "double" quotes."""
print(quote_string)
quote_string = """This string includes both 'single' and "double" quotes.""" print(quote_string)
quote_string = """This string includes both 'single' and "double" quotes."""
print(quote_string)

Αποτέλεσμα εκτέλεσης στην οθόνη:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This string includes both 'single' and "double" quotes.
This string includes both 'single' and "double" quotes.
This string includes both 'single' and "double" quotes.

Παράδειγμα docstring σε μια συνάρτηση:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def my_function():
"""This function does something important.
Here you can explain in detail what the function does.
"""
pass
def my_function(): """This function does something important. Here you can explain in detail what the function does. """ pass
def my_function():
    """This function does something important.

    Here you can explain in detail what the function does.
    """
    pass

Στην περίπτωση των docstrings, δεν υπάρχει άμεσο αποτέλεσμα εκτέλεσης στην οθόνη, καθώς αυτά χρησιμοποιούνται κυρίως για τεκμηρίωση και δεν είναι ορατά κατά την κανονική εκτέλεση του κώδικα.

Συμπερίληψη Εισαγωγικών σε Strings στην Python

Στην Python, όταν δημιουργείτε ένα string με μονά εισαγωγικά (‘), μπορείτε να περιλάβετε χαρακτήρες διπλών εισαγωγικών (“) μέσα στο string, αλλά όχι μονά εισαγωγικά, εκτός αν χρησιμοποιήσετε την ακολουθία διαφυγής (\). Για παράδειγμα, η προσπάθεια να χρησιμοποιηθεί ένα μονό εισαγωγικό μέσα σε ένα string που είναι περικλεισμένο σε μονά εισαγωγικά θα προκαλέσει σφάλμα σύνταξης (SyntaxError).

Αντίστοιχα, ένα string που είναι περικλεισμένο σε διπλά εισαγωγικά (“) μπορεί να περιέχει χαρακτήρες μονών εισαγωγικών, αλλά όχι διπλών, εκτός και πάλι αν χρησιμοποιηθεί η αντίστοιχη ακολουθία διαφυγής (\").

Για να αποφύγετε τη χρήση των ακολουθιών διαφυγής \' και \" μέσα σε strings, μπορείτε να περικλείσετε τα strings σε triple quotes (είτε μονά είτε διπλά).

Παράδειγμα με μονά εισαγωγικά και ακολουθία διαφυγής:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
single_quote_string = 'He said, \"Hello, I\'m here!\"'
print(single_quote_string)
single_quote_string = 'He said, \"Hello, I\'m here!\"' print(single_quote_string)
single_quote_string = 'He said, \"Hello, I\'m here!\"'
print(single_quote_string)

Αποτέλεσμα εκτέλεσης στην οθόνη:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
He said, "Hello, I'm here!"
He said, "Hello, I'm here!"
He said, "Hello, I'm here!"

Παράδειγμα με triple quotes για την αποφυγή ακολουθιών διαφυγής:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
triple_quoted_string = """He said, "Hello, I'm here!" """
print(triple_quoted_string)
triple_quoted_string = """He said, "Hello, I'm here!" """ print(triple_quoted_string)
triple_quoted_string = """He said, "Hello, I'm here!" """
print(triple_quoted_string)

Αποτέλεσμα εκτέλεσης στην οθόνη:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
He said, "Hello, I'm here!"
He said, "Hello, I'm here!"
He said, "Hello, I'm here!"

Πολυγραμμικά Strings στην Python

Στην Python, ένα πολυγραμμικό string (multiline string) μπορεί να δημιουργηθεί χρησιμοποιώντας triple-quoted strings. Αυτό σημαίνει ότι ξεκινάτε και τελειώνετε το string με τρία διπλά ή τρία μονά εισαγωγικά. Όταν πληκτρολογείτε ένα τέτοιο string στο IPython (ή άλλο διερμηνέα Python), και δεν ολοκληρώσετε το string με τα κλείσιμα εισαγωγικά πριν πατήσετε Enter, το IPython παρέχει ένα σύμβολο συνέχειας (π.χ. ...:), στο οποίο μπορείτε να εισάγετε την επόμενη γραμμή του string. Αυτό συνεχίζεται μέχρι να εισάγετε τα κλείσιμα εισαγωγικά και να πατήσετε Enter.

Όταν αποθηκεύετε ένα πολυγραμμικό string, η Python ενσωματώνει χαρακτήρες νέας γραμμής (\n) εκεί που πατήσατε Enter. Αν αξιολογήσετε το triple_quoted_string χωρίς να το εκτυπώσετε, το IPython θα το εμφανίσει με μονά εισαγωγικά και θα περιλαμβάνει τον χαρακτήρα \n στα σημεία όπου πατήσατε Enter. Τα εισαγωγικά που εμφανίζει το IPython δείχνουν ότι το triple_quoted_string είναι ένα string και δεν αποτελούν μέρος του περιεχομένου του string.

Για παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
triple_quoted_string = """This is an example
of a multiline string,
covering several lines."""
triple_quoted_string = """This is an example of a multiline string, covering several lines."""
triple_quoted_string = """This is an example
of a multiline string,
covering several lines."""

Αν αξιολογήσετε το triple_quoted_string στο IPython, θα δείτε κάτι σαν το παρακάτω:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
'This is an example\nof a multiline string,\ncovering several lines.'
'This is an example\nof a multiline string,\ncovering several lines.'
'This is an example\nof a multiline string,\ncovering several lines.'

Εδώ, το \n δηλώνει την αλλαγή γραμμής στο αρχικό πολυγραμμικό string.

10 Ιανουαρίου, 2024
top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων