rstrip() - strip out newlines/whitespace at the end of a stringsplit() - split a string into a list of strings based on a delimeterindex() - find a substring inside a stringcount() - count the number of times a substring appears in a stringIt's always good to be aware of a whole bunch of string methods - they can come in very handy.
See https://www.w3schools.com/python/python_ref_string.asp
Here's a sample:
name = "Eric"
#check if all the characters are lowercase
name.islower()
lowercase_name = name.lower()
lowercase_name
name1 = "eric"
name2 = "Titus"
name1.lower() < name2.lower()
name1 = "eric"
name1.capitalize()
user_input = float(input("Enter a number: "))
print("Your number divided by 2 is",user_input/2)
Here's a fix
user_input = input("Enter a number: ")
if user_input.isnumeric():
user_input = float(user_input)
print("Your number divided by 2 is",user_input/2)
else:
print("That's not a number!")
Perhaps you have tried to do something like this, and you end up with a space between the $ and the number that you don't want.
employee_num = 123
pay = 980.543
print("Employee",employee_num,"made $",pay,"this period.")
format() method¶The format() method allows you to substitute values into placeholders within a string.
employee_num = 123
pay = 980.543
pay_message = "Employee {n} made ${p} this pay period."
print( pay_message.format(n=employee_num, p=pay) )
You can even do it in one step:
employee_num = 123
pay = 980.543
print( "Employee {n} made ${p} this pay period.".format(n=employee_num, p=pay) )
You can even add formatting information to the placeholders for things like how to format numbers.
In this example ":.2f" means format as a floating-point number with 2 places to the right of the decimal point.
employee_num = 123
pay = 980.543
print( "Employee {n} made ${p:.2f} this pay period.".format(n=employee_num, p=pay) )
":.0%" means format as a percentage with 0 decimal places
print("{name} earned a {perc_grade:.0%} on the test".format(name="Eric",perc_grade=.897))
You just have to know it's a thing, and look it up when you need to use it.
Every text character is represented by a numerical code in your computer's memory.
They came up with in the 1960's as part of the ASCII standard (https://en.wikipedia.org/wiki/ASCII)
Here's some examples
|
|
|
You can use the built-in ord() function to look up the code for any given character. Use chr() to do the opposite.
ord("A")
chr(65)
In the lab, we are going to use the functions to do some encryption on strings.