Einführung in Python für die Softwareentwicklung
George Boorman
Curriculum Manager, DataCamp
# This works
my_text = 'Hello, my name is George.'
# This also works
my_text = "Hello, my name is George."
'
= Apostroph = einfaches 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.
Methode = eine Funktion, die nur für einen bestimmten Datentyp verfügbar ist
str
-Methoden
# Calling a string method
str_variable.method()
.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.
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
nineteen_eightyfour = "It was a bright cold day in April, and the clocks were striking thirteen."
# 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 StringsSyntax | 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() |
Einführung in Python für die Softwareentwicklung