Basics 12/01 - Strings

Run Settings
LanguagePython
Language Version
Run Command
# # Expressions Vs Statements # iq = 100 # this entire line is a statement # user_age = iq / 5 # "iq / 5" is an expression, that produces a value. In this case the expression is within a statement # # a statement is an entire line of code that performs an action, in the above case the entire line is a statement # # Augmented Assignment Operator # # some_value = 5 # # some_value = some_value + 2 # # some_value += 2 # shorthand version of the above. To work the variable requires a pre-existing value # # # works with several operators e.g +,-,*,/ - these go to the left of the = sign # # # print(some_value) # # Strings - Str - a string is simply a line of text # "such as this," # 'or this! You can even put numbers like 73 in it!' # # for individual variables on a single line # username = 'stinkycoder' # password = 'goongaginga' # # triple quotes for strings that cover multiple lines of text # long_string = ''' # WOW # O O # ___ # ''' # # print(long_string) # first_name = 'bob' # last_name = 'billy' # full_name = first_name + ' ' + last_name # the additional quotes allow for a space between names # print(full_name) # # String Concatenation - simply, adding strings together # print('hello' + 'bob') # only works between strings, e.g. could not do hello + 5 like this #type conversion # print(type(str(100))) # 100 has been converted into a string # print(type(int(str(100)))) # 100 has been converted into a string, then back to an int # a = str(100) # b = int(a) # c = type(b) # print(c) # performs same function as our previous conversion, using variables # # Escape Sequence - issues with double quotes and similar # # weather = "it's "kind of" sunny" - situations like this don't work # weather = "\it\'s \"kind of\" sunny" # using backslash \ allows python to see that this is a full string # # other special functions: \t creates a tab spacing effect, \n creates a new line # print(weather) #Formatted strings - for more dynamic situations e.g. changing names etc #name = 'Jimbo' #age = '73' # print('Hi' + ' ' + name + '. You are ' + str(age) + ' years old!') # works but is extremely clunky # print(f'hi {name}. You are {age} years old.') # significantly cleaner version using f to denote formatting, and {} for variables # print('hi {}. You are {} years old.'.format(name, age)) # alternative method - using numbers from 0 can change rotation of info # print('hi {new_name}. You are {age} years old.'.format(new_name = 'sally', age = 100)) # also works #.format slightly more complicated to deal with than just using f at the start of a string, but may be common in older code # # String Indexes # selfish = 'me me me' # #01234567 # each shelf corresponds to a value in the sting, e.g 0 = m, 1 = e, 3 = space etc # print(selfish[0]) # this will print the letter "m", by finding the data indexed at that point in the string # # [start] first item in the brackets is the start of the search # # [start:stop] allows you to set a place to end the search as well # print(selfish[0:4]) # prints items 0-4 in the variable, in this case "me m" # # [start:stop:stepover] stepover allows you to skip a certain number # print(selfish[0:8:2]) # in this case the step is 2, so it will skip every second item # print(selfish[-1]) # this will effectively start at the end and work backwards, e.g. here it will start at "e" which is 7th # print(selfish[::-1]) # this will entirely reverse the string, on higher numbers will skip as usual backwards instead # Immutability - cannot be changed. Strings are immutable. The only way to change it is to completely reassign variable
Editor Settings
Theme
Key bindings
Full width
Lines