Mit Strings arbeiten

Einführung in Python für die Softwareentwicklung

George Boorman

Curriculum Manager, DataCamp

Strings sind überall!

Knäuel

 

  • Benutzerprofile

 

  • Suchmaschinen

 

  • Große Sprachmodelle
1 https://unsplash.com/@steve_j
Einführung in Python für die Softwareentwicklung

Python kennt einfache und doppelte Anführungszeichen

# This works
my_text = 'Hello, my name is George.'

# This also works
my_text = "Hello, my name is George."
  • ' = Apostroph = einfaches Anführungszeichen
Einführung in Python für die Softwareentwicklung

Vorteile von doppelten Anführungszeichen

# Single quote string variable containing an apostrophe
my_text = 'Hello, my name's George.'
SyntaxError: invalid syntax.
# Double quote string variable containing an apostrophe
my_text = "Hello, my name's George."
print(my_text)
Hello, my name's George.
Einführung in Python für die Softwareentwicklung

Methoden

  • Methode = eine Funktion, die nur für einen bestimmten Datentyp verfügbar ist

  • str-Methoden

# Calling a string method
str_variable.method()
Einführung in Python für die Softwareentwicklung

Teile von Strings ersetzen

  • .replace(text_to_be_replaced, text_to_change_it_to)
my_text = "Hello, my name's George."

# Replace George with John
my_text = my_text.replace("George", "John")

print(my_text)
Hello, my name's John.
  • Häufige Anwendungsfälle
    • Umformatieren, z. B. Leerzeichen in Unterstriche umwandeln
    • Tippfehler korrigieren oder entfernen
Einführung in Python für die Softwareentwicklung

Groß- und Kleinschreibung verändern

current_top_album = "For All The Dogs"


# Convert to lowercase current_top_album = current_top_album.lower() print(current_top_album)
for all the dogs
# Change to upppercase
current_top_album = current_top_album.upper()
print(current_top_album)
FOR ALL THE DOGS
Einführung in Python für die Softwareentwicklung

Sätze und Absätze

nineteen_eightyfour = "It was a bright cold day in April, and the clocks were striking thirteen."
Einführung in Python für die Softwareentwicklung

Mehrzeilige Strings

# Create a string variable over multiple lines
harry_potter = """Mr. and Mrs. Dursley, 
of number four Privet Drive, 
were proud to say that they were perfectly normal, 
thank you very much.
"""
  • """text""": Mehrzeilige Strings
    • Verbessern die Lesbarkeit
    • Vermeiden die Verwendung von Sonderzeichen
    • Für längere Texte wie Kundenbewertungen
    • Werden benutzt, um zu erklären, was Funktionen machen
Einführung in Python für die Softwareentwicklung

String-Spickzettel

Syntax Zweck Beispiel
Einfache Anführungszeichen '' String-Variable my_string = 'My string'
Doppelte Anführungszeichen "" String-Variable my_string = "My string"
Dreifache Anführungszeichen """""" Mehrzeilige String-Variable my_string = """My string"""
str.replace() Teile von Strings ersetzen my_string = my_string.replace("text_to_remove", "text_to_change_to")
str.lower() Kleinbuchstaben-String my_string = my_string.lower()
str.upper() Großbuchstaben-String my_string = my_string.upper()
1 https://docs.python.org/3/library/stdtypes.html#string-methods
Einführung in Python für die Softwareentwicklung

Lass uns üben!

Einführung in Python für die Softwareentwicklung

Preparing Video For Download...