久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在python中將整數轉換為二進制

Converting integer to binary in python(在python中將整數轉換為二進制)
本文介紹了在python中將整數轉換為二進制的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

為了將整數轉換為二進制,我使用了以下代碼:

In order to convert an integer to a binary, I have used this code :

>>> bin(6)  
'0b110'

什么時候刪除'0b',我用這個:

and when to erase the '0b', I use this :

>>> bin(6)[2:]  
'110'

如果我想將 6 顯示為 00000110 而不是 110,該怎么辦?

What can I do if I want to show 6 as 00000110 instead of 110?

推薦答案

>>> '{0:08b}'.format(6)
'00000110'

只是解釋格式化字符串的部分:

Just to explain the parts of the formatting string:

  • {} 將變量放入字符串中
  • 0 獲取參數位置 0 處的變量
  • : 為該變量添加格式選項(否則它將表示十進制6)
  • 08 將數字格式化為 8 位數字,左側補零
  • b 將數字轉換為二進制表示
  • {} places a variable into a string
  • 0 takes the variable at argument position 0
  • : adds formatting options for this variable (otherwise it would represent decimal 6)
  • 08 formats the number to eight digits zero-padded on the left
  • b converts the number to its binary representation

如果您使用的是 Python 3.6 或更高版本,您還可以使用 f-strings:

If you're using a version of Python 3.6 or above, you can also use f-strings:

>>> f'{6:08b}'
'00000110'

這篇關于在python中將整數轉換為二進制的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 精品一区二区视频 | 69亚洲精品 | 午夜在线影院 | 国产一区二区三区四区 | 1区2区3区视频 | 国产欧美精品一区 | 久久精品免费一区二区三 | 欧美高清成人 | 超级黄色一级片 | 国产小视频精品 | 久久国产精品无码网站 | 天堂在线中文 | 欧美亚洲激情 | 美女天天操 | 伊人春色成人网 | 精品粉嫩aⅴ一区二区三区四区 | 久久亚洲精品久久国产一区二区 | 欧美福利 | 看毛片网站| 亚洲人成人一区二区在线观看 | 91免费小视频 | 免费在线一区二区三区 | 日韩成人在线播放 | 自拍亚洲 | 久久久久久久国产精品视频 | 日韩在线中文 | 欧美激情亚洲激情 | 一级做a爰片性色毛片 | 中文字幕一区二区三区四区 | 伊人久久综合 | 国产黄色小视频在线观看 | 嫩草视频在线看 | 天天看天天操 | 亚洲精品自在在线观看 | 三级在线免费 | 久久久免费电影 | 激情久久网 | 亚洲九色| 国产99久久精品一区二区300 | 成人黄色网址大全 | 婷婷色国产偷v国产偷v小说 |