之前向大家介紹過Python中float() 函數的實現過程(https://www.py.cn/jishu/jichu/21933.html)。在使用float() 函數時,我們用的字符是浮點數,但是有時我們並不需要浮點數,這時我們就要將float轉化為其他形式。本文將介紹Python float 轉換為 String的兩種方法:使用'%d'%num實現和使用str()方法實現。
方法一:使用'%d'%num實現
num = 322 str1 = '%d'%num # 轉為整型 print(str1) >>> '32' num = 32.348 str2 = '%f'%32.348 # 轉為浮點型 print(str2) >>> '32.348000'
輸出方法二:使用str()方法實現
pi = 3.1415 print(type(pi)) # float piInString = str(pi) # float -> str print(type(piInString)) # str
以上就是Python中將float 轉換為 String的兩種方法,都是很簡單的方法,大家可以選擇喜歡喜歡的方法進行轉換哦
原文至:https://www.py.cn/faq/python/21936.html