13 KiB
13 KiB
In [ ]:
# Single word
'hello'In [ ]:
# Entire phrase
'This is also a string'In [ ]:
# We can also use double quote
"String built with double quotes"In [ ]:
# Be careful with quotes!
' I'm using single quotes, but this will create an error'In [ ]:
"Now I'm ready to use the single quotes inside a string!"In [ ]:
# We can simply declare a string
'Hello World'In [ ]:
# Note that we can't output multiple strings this way
'Hello World 1'
'Hello World 2'In [ ]:
print('Hello World 1')
print('Hello World 2')
print('Use \n to print a new line')
print('\n')
print('See what I mean?')In [ ]:
len('Hello World')In [ ]:
# Assign s as a string
s = 'Hello World'In [ ]:
#Check
sIn [ ]:
# Print the object
print(s) In [ ]:
# Show first element (in this case a letter)
s[0]In [ ]:
s[1]In [ ]:
s[2]In [ ]:
# Grab everything past the first term all the way to the length of s which is len(s)
s[1:]In [ ]:
# Note that there is no change to the original s
sIn [ ]:
# Grab everything UP TO the 3rd index
s[:3]In [ ]:
#Everything
s[:]In [ ]:
# Last letter (one index behind 0 so it loops back around)
s[-1]In [ ]:
# Grab everything but the last letter
s[:-1]In [ ]:
# Grab everything, but go in steps size of 1
s[::1]In [ ]:
# Grab everything, but go in step sizes of 2
s[::2]In [ ]:
# We can use this to print a string backwards
s[::-1]In [ ]:
sIn [ ]:
# Let's try to change the first letter to 'x'
s[0] = 'x'In [ ]:
sIn [ ]:
# Concatenate strings!
s + ' concatenate me!'In [ ]:
# We can reassign s completely though!
s = s + ' concatenate me!'In [ ]:
print(s)In [ ]:
sIn [ ]:
letter = 'z'In [ ]:
letter*10In [ ]:
sIn [ ]:
# Upper Case a string
s.upper()In [ ]:
# Lower case
s.lower()In [ ]:
# Split a string by blank space (this is the default)
s.split()In [ ]:
# Split by a specific element (doesn't include the element that was split on)
s.split('W')In [ ]:
'Insert another string with curly brackets: {}'.format('The inserted string')In [ ]:
In [ ]: