Code
  • 首页
  • cpu-architecture
  • performance
  • c++
  • branch-prediction
  • java
2023-11-10 08:01:11

如何在Python中将字符转换为整数,反之亦然?

pythontype-conversionintegerchar

给定一个字符,我想要得到它的ASCII值。

例如,对于字符a,我想要得到97,反之亦然。


当前回答

>>> ord('a')
97
>>> chr(97)
'a'
2009-04-01 05:21:39

其他回答

片名:

2009-04-01 05:21:22
>>> ord('a')
97
>>> chr(97)
'a'
2009-04-01 05:21:39

对于长弦,你可以用这个。

 ''.join(map(str, map(ord, 'pantente')))
2021-05-07 13:38:19

不是OP的问题,但考虑到标题如何在Python中将字符转换为整数,

int(num) <==> ord(num) - ord('0')

str(char) <==> ord(char) - ord('a')
2023-01-12 15:55:12

使用chr()和ord():

>>> chr(97)
'a'
>>> ord('a')
97
2009-04-01 05:22:00

推荐文章

  • 证书验证失败:无法获得本地颁发者证书
  • 当使用pip3安装包时,“Python中的ssl模块不可用”
  • 无法切换Python与pyenv
  • Python if not == vs if !=
  • 如何从scikit-learn决策树中提取决策规则?
  • 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
  • 将旋转的xtick标签与各自的xtick对齐
  • 使用C返回一个数组
  • 为什么元组可以包含可变项?
  • 如何合并字典的字典?
  • 如何创建类属性?
  • 不区分大小写的“in”
  • 在Python中获取迭代器中的元素个数
  • 解析日期字符串并更改格式
  • 使用try和。Python中的if

aliyun

最新文章

  • 如何删除默认的导航栏空间在SwiftUI导航视图
  • 我可以列出所有的标准Go包吗?
  • 证书验证失败:无法获得本地颁发者证书
  • 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
  • 打开同一目录两次
  • 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
  • NullInjectorError: AngularFirestore没有提供程序
  • 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
  • 从元组/数组值派生联合类型
  • 如何在Angular2中截断文本?
  • 安装tzdata非交互式
  • ionic 2 - Error在Android Studio中找不到Gradle的安装版本
  • 如何查看所有地区所有正在运行的Amazon EC2实例?
  • 当使用pip3安装包时,“Python中的ssl模块不可用”
  • 安装APK时出现错误

标签

cpu-architectureperformancec++branch-predictionjavaundogit-commitgitversion-controlgit-remotegit-pushgit-branchgit-pullgit-fetchpythongeneratoriteratorjsonmime-typescontent-typearraysjavascriptgit-addcode-formattingcstandards-complianceoperatorscommentsdynamic-memory-allocationstackmemory-managementheap-memorylanguage-agnosticoverwritebackground-colorbrowserhtmldomjqueryvisibilityuse-strictsyntaxjslintremote-branchgit-checkoutpython-moduleidiomsprogram-entry-pointnamespacesbranch
2025 code 京ICP备15047053号-1