Data Science Portfolio

Strings

Basics

Create a string with single quotes and assign it to a variable

my_str = 'Hello World'

print(my_str)
Hello World

Create a string with double quotes and assign it to a variable

my_str = "Hello World"

print(my_str)
Hello World

Accessing String Elements

Access substrings of a string

my_str = "Hello World"

print(my_str[0])

print(my_str[0:5])
H
Hello

Modifying Strings

Create a new string using a substring of another string

my_str = "Hello World"

new_str = my_str[:6] + "Universe"

print(new_str)
Hello Universe

String Formatting Operator

Simple string formatting example

print("My name is %s and I am %d years old." % ("John", 30))
My name is John and I am 30 years old.