Introduction to Python for Developers
George Boorman
Curriculum Manager, DataCamp
# This works
my_text = 'Hello, my name is George.'
# This also works
my_text = "Hello, my name is George."
'
= apostrophe = single quote# 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.
Method = a function that is only available to a specific data type
str
methods
# 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"""
: Multi-line stringsSyntax | Purpose | Example |
---|---|---|
Single quotes '' |
String variable | my_string = 'My string' |
Double quotes "" |
String variable | my_string = "My string" |
Triple quotes """""" |
Multi-line string variable | my_string = """My string""" |
str.replace() |
Replacing parts of strings | my_string = my_string.replace("text_to_remove", "text_to_change_to") |
str.lower() |
Lowercase string | my_string = my_string.lower() |
str.upper() |
Uppercase string | my_string = my_string.upper() |
Introduction to Python for Developers