# Homework 1: Determine whether a string is a decimal
def is_float(string):
string1 = str(string)
if string1.count('.') > 1: # Detect the number of decimal points in the string
return ' The string is not a decimal '
elif string1.isdigit(): # Checks whether a string is made up of numbers only , Returns if the string contains only numbers True Otherwise return to False
return ' The string is not a decimal '
else:
new_string = string1.split(".") # Divide characters by decimal point
first_num = new_string[0] # Take this after segmentation list The first element of
# Judge the number of minus signs and first_num The first element is not "-", If the number of minus signs equals 1 also firs_num The first element is "-", Then it's legal
if first_num.count( '-') == 1 and first_num[0] == '-':
first_num = first_num.replace('-','')
if first_num.isdigit() and new_string[1].isdigit():
return ' The string is decimal '
else:
return ' The string is not a decimal '
if __name__ == '__main__':
while True:
print(" Input Q Exit procedure ")
string = input(" Determine whether a string is a decimal , Please enter a string :")
if string.upper() == 'Q':
print(" You quit the program ")
break
print(is_float(string))
print('\n')
notes : The following references are from Novice tutorial
str = "this is string happy!!!";
sub = "i";
print("str.count(sub, 4, 30) : ", str.count(sub, 4, 30))
sub = "is";
print("str.count(sub) : ", str.count(sub))
# give the result as follows
# str.count(sub, 4, 30) : 2
# str.count(sub) : 2
str = "123456" # Only this string of numbers
print(str.isdigit())
# result :True
str1 = "this is string!!!"
print(str1.isdigit())
# result :False
str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print(str.split( )) # Space as separator , contain \n
print(str.split(' ', 1 )) # Space as separator , Separate into two
# Output results
# ['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
# ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
txt = "Google#Runoob#Taobao#Facebook"
x = txt.split("#", 1) # The second parameter is 1, Returns a list of two parameters
print(x)
# Output results : ['Google', 'Runoob#Taobao#Facebook']
str1 = "this is string!!!"
print("str.upper():",str.upper())
# Output results : str.upper(): THIS IS STRING HAPPY!!!
Articles you may be interested in :