Quick actions

cmd+k|ctrl+k

Navigation

Languages

formatted strings, string indexes and immutability

Snippet info

Language

Python

Visibility

public

Author

dylansiegelman

Created

2024-03-30T23:55:30.04496Z

Updated

2024-03-31T01:19:18.042874Z

name = 'dylan'
age = 6
print('hi ' + name + '. You are ' + str(age) + ' years old') # had to convert part to int

print(f'hi {name}. You are {age} years old') #the f in the beginning is for formatting
# makes you not have to convert
print('hi {}. You are {} years old'.format(name, age)) #this is python 2
print('hi {1}. You are {0} years old'.format(name, age))
print('hi {new_name}. You are {cool_age} years old'.format(new_name='pooper', cool_age='1000'))


order = 'abcdef'
        #012345
        #[start] these brackets mean start 
        #[start:stop] gives start stop option
        #[start:stop:stepover] to step over
print(order[0])
print(order[1])
print(order[0:3])
print(order[0:5:2])
print(order[1:])
print(order[:5])
print(order[::1])
print(order[-1]) #the minus means start at the end of the string
print(order[::-1])
print(order[::-2])
#strings are immutable, so you cant change just one of the characters in a string.
# youd have to assign a whole new value
order = '33'
print(order)
INFO