Validation and Checking (IT) Flashcards
Checking Minimum Length of a string
print(f”Enter String”)
string = input()
if len(string) > 5:
print(f”String okay”)
else:
print(f”String is too short”)
Check an empty string
print(f”Enter String”)
string = input()
if len(string) == 0:
print(f”String is empty”)
Checking if data entered is within the given range
number = int(input(“Enter a number”))
if number > 1 and number < 10:
print(f”Within range”)
else:
print(f”Not in range”)
Merging multiple strings
a = “Hello”
b = “World”
c = a + b
print(c)
Returning position of character
student = ‘Hermione’
student.index (i)
(Basically if I put in 2 ‘r’ is outputted)
Finding a character
student = ‘Hermione’
print (student[2]) (r)
Length of a string
len(“Hello”)
Letter to ASSCI character code
ord(a)
ASSCI character code to letter
chr(101)
From integers to floats
a = float(“12.3”)
Integer to string
a = str(12)
Real to string
a = str(12.3)
DIV (//)
a = 7 // 2 (3) (No remainder)
MOD %
a = 7 % 2 (1) (Remainder)
Outputting certain characters
Student = “Jeff Make”
print(Student [0:2])