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 Ο τύπος String στην Python

Στην Python, οι συμβολοσειρές (strings) αναφέρονται σε μια σειρά χαρακτήρων, και μπορούν να περικλείονται σε ενιαία ή διπλά εισαγωγικά. Είναι αναλλοίωτες (immutable), δηλαδή δεν μπορούν να τροποποιηθούν αφού δημιουργηθούν. Μπορούν να επεξεργαστούν με πολλούς τρόπους χρησιμοποιώντας ενσωματωμένες μεθόδους και συναρτήσεις.

Παρακάτω παραθέτουμε μερικά παραδείγματα στην Python σχετικά με τις συμβολοσειρές:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Δημιουργία ενός string
my_string = "Hello World!"
print(my_string) # Hello World!
# Μήκος του string
print(len(my_string)) # 12
# Πρόσβαση σε ένα συγκεκριμένο χαρακτήρα
print(my_string[0]) # H
print(my_string[-1]) # !
# Αλλαγή του χαρακτήρα στη θέση 6
my_string = my_string[:6] + 'Python'
print(my_string) # Hello Python!
# Εύρεση της θέσης ενός χαρακτήρα
print(my_string.index('P')) # 6
# Μετατροπή σε κεφαλαία/πεζά
print(my_string.upper()) # HELLO PYTHON!
print(my_string.lower()) # hello python!
# Έλεγχος αν ένα string ξεκινά με συγκεκριμένο χαρακτήρα
print(my_string.startswith('H')) # True
# Έλεγχος αν ένα string περιέχει ένα συγκεκριμένο υποσύνολο χαρακτήρων
print('Python' in my_string) # True
# Δημιουργία ενός string my_string = "Hello World!" print(my_string) # Hello World! # Μήκος του string print(len(my_string)) # 12 # Πρόσβαση σε ένα συγκεκριμένο χαρακτήρα print(my_string[0]) # H print(my_string[-1]) # ! # Αλλαγή του χαρακτήρα στη θέση 6 my_string = my_string[:6] + 'Python' print(my_string) # Hello Python! # Εύρεση της θέσης ενός χαρακτήρα print(my_string.index('P')) # 6 # Μετατροπή σε κεφαλαία/πεζά print(my_string.upper()) # HELLO PYTHON! print(my_string.lower()) # hello python! # Έλεγχος αν ένα string ξεκινά με συγκεκριμένο χαρακτήρα print(my_string.startswith('H')) # True # Έλεγχος αν ένα string περιέχει ένα συγκεκριμένο υποσύνολο χαρακτήρων print('Python' in my_string) # True
# Δημιουργία ενός string
my_string = "Hello World!"
print(my_string)  # Hello World!

# Μήκος του string
print(len(my_string))  # 12

# Πρόσβαση σε ένα συγκεκριμένο χαρακτήρα
print(my_string[0])  # H
print(my_string[-1])  # !

# Αλλαγή του χαρακτήρα στη θέση 6
my_string = my_string[:6] + 'Python'
print(my_string)  # Hello Python!

# Εύρεση της θέσης ενός χαρακτήρα
print(my_string.index('P'))  # 6

# Μετατροπή σε κεφαλαία/πεζά
print(my_string.upper())  # HELLO PYTHON!
print(my_string.lower())  # hello python!

# Έλεγχος αν ένα string ξεκινά με συγκεκριμένο χαρακτήρα
print(my_string.startswith('H'))  # True

# Έλεγχος αν ένα string περιέχει ένα συγκεκριμένο υποσύνολο χαρακτήρων
print('Python' in my_string)  # True

Στην Python, οι συμβολοσειρές είναι περικυκλωμένες με είτε μονά εισαγωγικά είτε διπλά εισαγωγικά.

Για παράδειγμα, οι παρακάτω δηλώσεις συμβολοσειράς είναι ισοδύναμες:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
x = 'hello'
y = "hello"
x = 'hello' y = "hello"
x = 'hello'
y = "hello"

Μπορείτε να εμφανίσετε μια συμβολοσειρά χρησιμοποιώντας τη συνάρτηση print(). Για παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
print("Hello, World!")
print("Hello, World!")
print("Hello, World!")

Επιπλέον, οι συμβολοσειρές στην Python μπορούν να συνενωθούν με τον τελεστή “+” και να επαναληφθούν με τον τελεστή “*”. Για παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
x = "Hello"
y = "World"
z = x + " " + y
print(z) # εκτύπωση: "Hello World"
x = "Hello"
y = x * 3
print(y) # εκτύπωση: "HelloHelloHello"
x = "Hello" y = "World" z = x + " " + y print(z) # εκτύπωση: "Hello World" x = "Hello" y = x * 3 print(y) # εκτύπωση: "HelloHelloHello"
x = "Hello"
y = "World"
z = x + " " + y
print(z)   # εκτύπωση: "Hello World"

x = "Hello"
y = x * 3
print(y)   # εκτύπωση: "HelloHelloHello"

Η ανάθεση ενός αλφαριθμητικού σε μια μεταβλητή γίνεται με το όνομα της μεταβλητής ακολουθούμενο από ένα ίσον και το αλφαριθμητικό:

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
my_string = "Αυτή είναι μια συμβολοσειρά!"
print(my_string)
my_string = "Αυτή είναι μια συμβολοσειρά!" print(my_string)
my_string = "Αυτή είναι μια συμβολοσειρά!"
print(my_string)

Το αποτέλεσμα στην οθόνη θα είναι:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Αυτή είναι μια συμβολοσειρά!
Αυτή είναι μια συμβολοσειρά!
Αυτή είναι μια συμβολοσειρά!

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

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
my_string = '''Αυτή είναι μια
πολλαπλών γραμμών
συμβολοσειρά'''
print(my_string)
my_string = '''Αυτή είναι μια πολλαπλών γραμμών συμβολοσειρά''' print(my_string)
my_string = '''Αυτή είναι μια
πολλαπλών γραμμών
συμβολοσειρά'''
print(my_string)

Αυτό θα εμφανίσει:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Αυτή είναι μια
πολλαπλών γραμμών
συμβολοσειρά
Αυτή είναι μια πολλαπλών γραμμών συμβολοσειρά
Αυτή είναι μια
πολλαπλών γραμμών
συμβολοσειρά

Μπορείτε να αντιστοιχίσετε ένα πολλαπλών γραμμών συμβολοσειρά σε μια μεταβλητή χρησιμοποιώντας τρεις μονά εισαγωγικά της μορφής ”’ ή τρεις διπλά εισαγωγικά της μορφής “”” :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
multiline_string = '''Καλημέρα
Σήμερα είναι μια όμορφη μέρα
Ας την αξιοποιήσουμε'''
print(multiline_string)
multiline_string = '''Καλημέρα Σήμερα είναι μια όμορφη μέρα Ας την αξιοποιήσουμε''' print(multiline_string)
multiline_string = '''Καλημέρα
Σήμερα είναι μια όμορφη μέρα
Ας την αξιοποιήσουμε'''

print(multiline_string)

Αποτέλεσμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Καλημέρα
Σήμερα είναι μια όμορφη μέρα
Ας την αξιοποιήσουμε
Καλημέρα Σήμερα είναι μια όμορφη μέρα Ας την αξιοποιήσουμε
Καλημέρα
Σήμερα είναι μια όμορφη μέρα
Ας την αξιοποιήσουμε

Στην Python, τα strings είναι σαν πίνακες από bytes που αναπαριστούν τους Unicode χαρακτήρες.

Ωστόσο, στην Python δεν υπάρχει τύπος δεδομένων για έναν μεμονωμένο χαρακτήρα. Ένας μεμονωμένος χαρακτήρας είναι απλά ένα string με μήκος 1.

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

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
my_string = "Hello, World!"
print(my_string[0]) # Output: H
print(my_string[7]) # Output: W
my_string = "Hello, World!" print(my_string[0]) # Output: H print(my_string[7]) # Output: W
my_string = "Hello, World!"
print(my_string[0])  # Output: H
print(my_string[7])  # Output: W

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
my_string = "Hello, World!"
print(my_string[-1]) # Output: !
print(my_string[-6]) # Output: W
my_string = "Hello, World!" print(my_string[-1]) # Output: ! print(my_string[-6]) # Output: W
my_string = "Hello, World!"
print(my_string[-1])  # Output: !
print(my_string[-6])  # Output: W

Δεδομένου ότι οι συμβολοσειρές στην Python είναι πίνακες (arrays) από bytes που αναπαριστούν χαρακτήρες Unicode, μπορούμε να επαναλάβουμε τους χαρακτήρες μιας συμβολοσειράς με ένα for loop.

Παραδειγμα 1: Χρησιμοποιώντας τον βρόχο for για να εκτυπώσουμε κάθε χαρακτήρα της συμβολοσειράς.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
my_string = "Hello World!"
for char in my_string:
print(char)
my_string = "Hello World!" for char in my_string: print(char)
my_string = "Hello World!"

for char in my_string:
    print(char)

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
H
e
l
l
o
W
o
r
l
d
!
H e l l o W o r l d !
H
e
l
l
o

W
o
r
l
d
!

Παραδειγμα 2: Χρησιμοποιώντας τον βρόχο for για να επεξεργαστούμε κάθε χαρακτήρα της συμβολοσειράς.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
my_string = "Hello World!"
new_string = ""
for char in my_string:
if char != "o":
new_string += char
print(new_string)
my_string = "Hello World!" new_string = "" for char in my_string: if char != "o": new_string += char print(new_string)
my_string = "Hello World!"
new_string = ""

for char in my_string:
    if char != "o":
        new_string += char

print(new_string)

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hell Wrld!
Hell Wrld!
Hell Wrld!

Για να πάρετε το μήκος ενός string, χρησιμοποιήστε τη συνάρτηση len().

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
string = "Hello World"
print(len(string)) # εκτυπώνει: 11
string = "Hello World" print(len(string)) # εκτυπώνει: 11
string = "Hello World"
print(len(string)) # εκτυπώνει: 11

Σε αυτό το παράδειγμα, η μεταβλητή string περιέχει τη συμβολοσειρά "Hello World". Το μήκος της συμβολοσειράς υπολογίζεται με τη χρήση της συνάρτησης len() και εκτυπώνεται στη συνέχεια στην οθόνη.

ια να ελέγξουμε αν μια συγκεκριμένη φράση ή χαρακτήρας είναι παρόν σε ένα string, μπορούμε να χρησιμοποιήσουμε τη λέξη-κλειδί “in”.

Αν ο χαρακτήρας ή η φράση είναι παρόν στο string, η συνθήκη θα επιστρέψει True, διαφορετικά θα επιστραφεί False.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Check if "world" is present in the string
txt = "Hello, world"
if "world" in txt:
print("Yes, 'world' is present in the string.")
# Check if "apple" is NOT present in the string
txt = "Banana, orange, grape"
if "apple" not in txt:
print("No, 'apple' is not present in the string.")
# Check if "world" is present in the string txt = "Hello, world" if "world" in txt: print("Yes, 'world' is present in the string.") # Check if "apple" is NOT present in the string txt = "Banana, orange, grape" if "apple" not in txt: print("No, 'apple' is not present in the string.")
# Check if "world" is present in the string
txt = "Hello, world"
if "world" in txt:
    print("Yes, 'world' is present in the string.")

# Check if "apple" is NOT present in the string
txt = "Banana, orange, grape"
if "apple" not in txt:
    print("No, 'apple' is not present in the string.")

Αν τρέξετε αυτόν τον κώδικα, θα δείτε τα παρακάτω αποτελέσματα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Yes, 'world' is present in the string.
No, 'apple' is not present in the string.
Yes, 'world' is present in the string. No, 'apple' is not present in the string.
Yes, 'world' is present in the string.
No, 'apple' is not present in the string.

Για να ελέγξουμε αν ένα συγκεκριμένο κείμενο ή χαρακτήρας ΔΕΝ υπάρχει σε μια συμβολοσειρά, μπορούμε να χρησιμοποιήσουμε τη λέξη-κλειδί “not in”.

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
text = "Hello World"
if "apple" not in text:
print("The word 'apple' is not present")
text = "Hello World" if "apple" not in text: print("The word 'apple' is not present")
text = "Hello World"
if "apple" not in text:
  print("The word 'apple' is not present")

Αυτό θα εμφανίσει το ακόλουθο κείμενο:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The word 'apple' is not present
The word 'apple' is not present
The word 'apple' is not present
top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων