Σtring στο Python

String literals

The string literals in Python are enclosed by single or double quotes.

hello είναι ίσο με "hello".

Μπορείτε να χρησιμοποιήσετε print() Η συνάρτηση εμφανίζει την αλφαβητική γραμμή:

Example

print("Hello")
print('Hello')

Run an instance

Αποθήκευση αλφαβητικής γραμμής σε μεταβλητή με αλφαβητική γραμμή

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

Example

a = "Hello"
print(a)

Run an instance

Αλφαβητικές γραμμές σε πολλές γραμμές

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

Example

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

a = """Python είναι μια ευρέως χρησιμοποιούμενη γενική, υψηλού επιπέδου γλώσσα προγραμματισμού. 
Αρχικά σχεδιάστηκε από τον Guido van Rossum το 1991 
και αναπτύχθηκε από την Python Software Foundation. 
Η ανάπτυξή του ήταν κυρίως για την έμφαση στην ευκολία ανάγνωσης του κώδικα. 
και η γλώσσα του επιτρέπει στους προγραμματιστές να εκφράσουν έννοιες σε λιγότερες γραμμές κώδικα.
print(a)

Run an instance

ή τρεις μοναδικές εισαγωγικές σημειώσεις:

Example

a = '''Python είναι μια ευρέως χρησιμοποιούμενη γενική, υψηλού επιπέδου γλώσσα προγραμματισμού. 
Αρχικά σχεδιάστηκε από τον Guido van Rossum το 1991 
και αναπτύχθηκε από την Python Software Foundation. 
Η ανάπτυξή του ήταν κυρίως για την έμφαση στην ευκολία ανάγνωσης του κώδικα. 
και η γλώσσα του επιτρέπει στους προγραμματιστές να εκφράσουν έννοιες σε λιγότερες γραμμές κώδικα.
print(a)

Run an instance

Note:Στα αποτελέσματα, η παύλα εισάγεται στη θέση που αντιστοιχεί στο κώδικα.

Οι αλφαβητικές γραμμές είναι μαγνητοσκοπίες

Παρόμοια με πολλές άλλες δημοφιλείς γλώσσες προγραμματισμού, οι αλφαβητικές γραμμές στο Python είναι μπλοκ byte που εκπροσωπούν unicode χαρακτήρες.

Ωστόσο, το Python δεν έχει τύπο χαρακτήρα, ένας χαρακτήρας είναι μια αλφαβητική γραμμή με μήκος 1.

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

Example

Αποκτήστε τον χαρακτήρα στη θέση 1 (μπορείτε να θυμάστε ότι η θέση του πρώτου χαρακτήρα είναι 0):

a = "Hello, World!"
print(a[1])

Run an instance

Κόψιμο

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

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

Example

Αποκτήστε τους χαρακτήρες από τη θέση 2 μέχρι τη θέση 5 (μη περιλαμβανομένης της θέσης 5):

b = "Hello, World!"
print(b[2:5])

Run an instance

Αρνητικές αναφορές

Χρησιμοποιήστε αρνητική αναφορά για να κόψετε την αλφαβητική γραμμή από το τέλος:

Example

Αποκτήστε τους χαρακτήρες από τη θέση 5 μέχρι τη θέση 1, μετρώντας από το τέλος της αλφαβητικής γραμμής:

b = "Hello, World!"
print(b[-5:-2])

Run an instance

Μήκος αλφαριθμητικής συμβολοσειράς

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

Example

Η συνάρτηση len() επιστρέφει την μήκος της συμβολοσειράς:

a = "Hello, World!"
print(len(a))

Run an instance

String Methods

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

Example

Η μέθοδος strip() αφαιρεί τα κενά από την αρχή και το τέλος της συμβολοσειράς:

a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"

Run an instance

Example

Η μέθοδος lower() επιστρέφει τη συμβολοσειρά σε πεζά γράμματα:

a = "Hello, World!"
print(a.lower())

Run an instance

Example

Η μέθοδος upper() επιστρέφει τη συμβολοσειρά σε κεφαλαία γράμματα:

a = "Hello, World!"
print(a.upper())

Run an instance

Example

Η μέθοδος replace() αντικαθιστά τη συμβολοσειρά με μια άλλη συμβολοσειρά:

a = "Hello, World!"
print(a.replace("World", "Kitty"))

Run an instance

Example

Η μέθοδος split() διασπά τη συμβολοσειρά σε υποσymbολοσειρές όταν βρίσκει το παράδειγμα του διαχωριστικού:

a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']

Run an instance

Χρησιμοποιήστε το χειροκίνητο εγχειρίδιο των μεθόδων αλφαριθμητικών συμβολοσειρών μας για να μάθετε περισσότερα για τις μεθόδους.

Ελέγχος αλφαριθμητικής συμβολοσειράς

Για να ελέγξετε αν υπάρχει συγκεκριμένη φράση ή χαρακτήρας σε μια αλφαριθμητική συμβολοσειρά, μπορούμε να χρησιμοποιήσουμε τις λέξεις-κλειδιά in ή not in.

Example

Ελέγξτε αν η φράση "ina" υπάρχει στο παρακάτω κείμενο:

txt = "China is a great country"
x = "ina" in txt
print(x)

Run an instance

Example

Ελέγξτε αν η φράση "ina" δεν υπάρχει στο παρακάτω κείμενο:

txt = "China is a great country"
x = "ain" not in txt
print(x) 

Run an instance

Σειρά αλφαριθμητικών συμβολοσειρών (συνδυασμός)

Για τη σειρά ή συνδυασμό δύο αλφαριθμητικών συμβολοσειρών, μπορείτε να χρησιμοποιήσετε τον τύπο +.

Example

Συνδυάζοντας τις μεταβλητές a και b στη μεταβλητή c:

a = "Hello"
b = "World"
c = a + b
print(c)

Run an instance

Example

Προσθέτοντας ένα κενό μεταξύ τους:

a = "Hello"
b = "World"
c = a + " " + b
print(c)

Run an instance

Μορφοποίηση αλφαριθμητικής συμβολοσειράς

Ωστόσο, όπως μάθαμε στο κεφάλαιο για τις μεταβλητές Python, δεν μπορούμε να συνδυάσουμε αλφαριθμητικές συμβολοσειρές και αριθμούς ως εξής:

Example

ηλός = 63
txt = "My name is Bill, I am " + ηλός
print(txt)

Run an instance

Αλλά μπορούμε να χρησιμοποιήσουμε format() Η μέθοδος συνδυάζει αλφαριθμητικές συμβολοσειρές και αριθμούς!

format() Η μέθοδος αποδέχεται τα παραδοθέντα παραμέτρων, τα μορφοποιεί και τα τοποθετεί σε σημεία υποκαταστάσεως {} στην αλφαριθμητική συμβολοσειρά που βρίσκεται:

Example

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

ηλός = 63 
txt = "My name is Bill, and I am {}"
print(txt.format(age))

Run an instance

format() Methods accept an unlimited number of parameters and place them in their respective placeholders:

Example

quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))

Run an instance

You can use the index number {0} To ensure that the parameters are placed in the correct placeholders:

Example

quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))

Run an instance

String Methods

Python has a set of built-in methods that can be used on strings.

Note:All string methods return a new value. They do not change the original string.

Method Description
capitalize() Convert the first character to uppercase.
casefold() Convert the string to lowercase.
center() Return the centered string.
count() Return the number of times the specified value appears in the string.
encode() Return the encoding version of the string.
endswith() If the string ends with the specified value, return true.
expandtabs() Set the tab size of the string.
find() Search for the specified value in the string and return the position where it is found.
format() Format the specified value in the string.
format_map() Format the specified value in the string.
index() Search for the specified value in the string and return the position where it is found.
isalnum() If all characters in the string are alphanumeric, return True.
isalpha() If all characters in the string are in the alphabet, return True.
isdecimal() If all characters in the string are decimals, return True.
isdigit() If all characters in the string are digits, return True.
isidentifier() If the string is an identifier, return True.
islower() If all characters in the string are lowercase, return True.
isnumeric() If all characters in the string are numbers, return True.
isprintable() If all characters in the string are printable, return True.
isspace() If all characters in the string are whitespace characters, return True.
istitle() Return True if the string follows the title rules.
isupper() Return True if all characters in the string are uppercase.
join() Concatenate the elements of an iterable object to the end of the string.
ljust() Return the left-aligned version of the string.
lower() Convert the string to lowercase.
lstrip() Return the left-trimmed version of the string.
maketrans() Return the translation table used in the conversion.
partition() Return a tuple where the string is divided into three parts.
replace() Return a string where the specified value is replaced with the specified value.
rfind() Search for the specified value in the string and return the last position where it is found.
rindex() Search for the specified value in the string and return the last position where it is found.
rjust() Return the right-aligned version of the string.
rpartition() Return a tuple, where the string is divided into three parts.
rsplit() Split the string at the specified delimiter and return a list.
rstrip() Return the right-trimmed version of the string.
split() Split the string at the specified delimiter and return a list.
splitlines() Split the string at the newline character and return a list.
startswith() Return true if the string starts with the specified value.
strip() Return the truncated version of the string.
swapcase() Switch case, lowercase becomes uppercase, and vice versa.
title() Convert the first character of each word to uppercase.
translate() Return the converted string.
upper() Convert the string to uppercase.
zfill() Fill the beginning of the string with a specified number of 0 values.

Note:All string methods return a new value. They do not change the original string.