程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python exercise - determining whether a string is a decimal

編輯:Python
# 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')

Knowledge points involved :

notes : The following references are from Novice tutorial

  • count()
    describe
    Python count() Method is used to count the number of characters in a string . The optional parameters are at the beginning and end of the string search .
    grammar
    count() Method syntax :str.count(sub,start=0,end=len(string))
    Parameters
    sub - - Search for substrings .
    start - - Where the string starts searching . The default is the first character , The first character index value is 0.
    end - - Where to end the search in the string . The index of the first character in the character is 0. Default to the last position of the string .
    Return value
    This method returns the number of times the substring appears in the string .
    example
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
  • isdigit()
    describe
    Python isdigit() Method to detect whether the string consists of only numbers
    grammar
    isdigit() Method syntax :str.isdigit()
    Parameters
    nothing
    Return value
    Returns if the string contains only numbers True No one returns False
    example
str = "123456" # Only this string of numbers 
print(str.isdigit())
# result :True
str1 = "this is string!!!"
print(str1.isdigit())
# result :False
  • split()
    describe
    Python split() Slice a string by specifying a separator , If parameters num There is a specified value , Then separate num+1 Substring
    grammar
    split() Method syntax :str.split(str="",num=string.count(str))
    Parameters
    str - - Separator , The default is all empty strings , Including Spaces 、 Line break (
    n)、 tabs (\t) etc. .
    num - - Number of divisions . The default is -1, To separate all .
    Return value
    Returns a list of split strings
    example
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']
  • upper()
    describe
    Python upper() Method to convert lowercase letters in a string to uppercase letters .
    grammar
    upper() Method syntax :str.upper()
    Parameters
    NA
    Return value
    Returns a string that converts lowercase letters to uppercase letters .
    example
str1 = "this is string!!!"
print("str.upper():",str.upper())
# Output results : str.upper(): THIS IS STRING HAPPY!!!
  • break
    Python in break and continue Explain in detail the usage and differences of

Articles you may be interested in :

  1. Python Switch pip Mirror source ( Install source ) A detailed explanation of
  2. Python note 001-Python introduction

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved