简体   繁体   English

如何获取字符串python中的前x个字符

[英]how to grab the first x characters in a string python

I'm trying to get the first 8 characters in the string by using the following code :我正在尝试使用以下代码获取字符串中的前 8 个字符:

integer = int('789ABC', 16)

conver = format(integer, '0>24b')

x1 = conver.split()[:8]

but when I printing the x1,但是当我打印 x1 时,

 ['011110001001101010111100']

result didn't came out as what I wanted.结果并没有如我所愿。

Result expecting :01111000结果期待:01111000

split() is unnecessary and ends up just putting conver inside a list as the only element. split()是不必要的,最终只是将conver作为唯一元素放在列表中。

integer = int('789ABC', 16)

conver = format(integer, '0>24b')

x1 = conver[:8]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM