Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Adobe Connect 4.10.2021, string manipulation and error handling
- print("Welcome!")
- text = input("Give some text:\n")
- print(text)
- # slice from beginning
- substring = text[0:8]
- print(substring)
- # slice from middle of sentence
- substring2 = text[3:10]
- print(substring2)
- # NEW FILE
- # removing an extra character from the end
- # we can use a negative approach
- # -1 => second last character
- years = "2021-2020-2019-2018-"
- years = years[0:-1]
- print(years)
- # NEW FILE
- text = input("Give some text!\n")
- # getting the amonut of characters in a string
- text_length = len(text)
- print(f"Your text has {text_length} characters!")
- print()
- # check if your text is empty
- # you can also compare to ''
- if text_length == 0:
- print("YOUR TEXT IS EMPTY.")
- # if/else, short or long text?
- if text_length > 10:
- print("That's a long piece of text!")
- else:
- print("That text is a bit short, isn't it?")
- # VERSION 2:
- # check if your text is empty
- # you can also compare to ''
- if text_length == 0:
- print("YOUR TEXT IS EMPTY.")
- else:
- # if/else, short or long text?
- if text_length > 10:
- print("That's a long piece of text!")
- else:
- print("That text is a bit short, isn't it?")
- # NEW FILE
- text = input("Give some text!\n")
- # force all letters to lowercase
- text = text.lower()
- print(text)
- # count a's and o's in sentence
- a_letters = text.count("a")
- o_letters = text.count("o")
- total = a_letters + o_letters
- # print out the results
- print(f"Your sentence has {a_letters} a-letters!")
- print(f"Your sentence has {o_letters} o-letters!")
- # reverse the text
- reversed_text = text[::-1]
- print(reversed_text)
- # NEW FILE
- # replacing a word
- drinks = "water, milk, tea, coffee, soda"
- new_text = drinks.replace("soda", "juice")
- print(new_text)
- # replace replaces all occurences by default
- drinks = "water, milk, water, coffee, tea"
- new_text = drinks.replace("water", "soda")
- print(new_text)
- # replace only one
- drinks = "water, milk, water, coffee, tea"
- new_text = drinks.replace("water", "soda", 1)
- print(new_text)
- # this is also possible
- new_text = new_text.replace(" ", "\n")
- print(new_text)
- # NEW FILE
- drinks = "water, milk, coffee, tea"
- choice = input("What would you like to drink?\n")
- # check if users word is in the text
- if choice in drinks:
- print("Word found!")
- else:
- print("Word not found...")
- # NEW FILE
- text = input("Give text or number:\n")
- # we can react with an if-statement
- # if user gave text or a number!
- # => your application can work differently
- # depending on this!
- if text.isnumeric():
- print("User gave you a number!")
- number = int(text)
- number = number + 100
- print(number)
- else:
- print("User gave you text.")
- # NEW FILE
- try:
- number = input("Give a number:\n")
- number = int(number)
- number = number + 10
- print(number)
- print("This is my application!")
- except ValueError:
- print("You wrote text! Please give a number.")
- # NEW FILE
- try:
- # ask two numbers from user
- number1 = input("Give a number:\n")
- number1 = int(number1)
- number2 = input("Give another number:\n")
- number2 = int(number2)
- # division
- number = number1 / number2
- print(number)
- print("This is my application!")
- except ValueError:
- # user wrote text
- print("You wrote text! Please give a number.")
- except ZeroDivisionError:
- # seconds number was zero => divided by zero
- print("You divided by zero!")
- except Exception as e:
- # something else happened...error anyways.
- print(str(e))
- print("There was an error.")
- # application doesn't crash and this is printed too!
- print("Thanks for using the application!")
- # NEW FILE
- # example, a client id follows this format: C1324_4356
- try:
- client = input("Give a client id:")
- # the length of the client id should be 10 characters
- text_length = len(client)
- # client id should be 10 characters long
- # and 6th character should be an underscore!
- if text_length != 10:
- print("Client id length not correct.")
- elif client[5] != "_":
- print("Underscore is missing!")
- else:
- # everything seems to be okay
- id = client[0:5]
- order = client[6:10]
- order = int(order)
- print(id)
- print(order)
- except Exception as e:
- print("Error: " + str(e))
Add Comment
Please, Sign In to add comment