Strings

Run Settings
LanguagePython
Language Version
Run Command
numbers = '0123456789' # [start:stop:stepover] print(numbers[0:7:2]) # will print from index 0 to index 8 in jumps of 2 - 0246 print(numbers[:5]) # will print the first 5 characters, from index 0 to index 4 - 01234 print(numbers[5:]) # will print from index 5 to the end of the string - 56789 print(numbers[::2]) # will print all the characters that are in even indexes - 02468 print(numbers[-1]) # This will start from the end - prints 9 # If we want to reverse the string we can write it like this: print(numbers[::-1]) # 9876543210 print(numbers[6:3:-1]) # 654 print(len(numbers)) # 10 # String methods: quote = 'to be or not to be. that is the question' print(quote.upper()) # TO BE OR NOT TO BE print(quote.capitalize()) # Turns only the first letter to a capitalized letter - To be or not to be.that is the question print(quote.find('be')) # If the substring exists in quotes, return the start index - 3 print(quote.replace('be', 'me')) # Replaces all the 'be' occurences with 'me' - to me or not to me print(quote) # Will print to be or not to be since strings are immutable. Can be changed only by assignment ### Booleans print(bool(0)) # False - 0 will return False. All other values (including negatives) will return True print(bool(1)) # True print(bool(23)) # True print(bool(-13)) # True ### Exercise: birth_year = int(input('What year were you born?')) print(f'Your age is: {2026 - birth_year}') ### Exercise 2: username = input('Please enter your user name:\n') password = input('Please enter your password:\n') hidden_password = '*' * len(password) print(f'{username}, your password {hidden_password} is {len(password)} letters long.')
Editor Settings
Theme
Key bindings
Full width
Lines