简体   繁体   English

在python中切片字符串并将其转换为整数

[英]Slicing a string in python and converting it to an integer

I've been trying to sort registration numbers.我一直在尝试对注册号进行排序。 The registration number is of the format 18[az][az][az][0-9][0-9][0-9][0-9]注册号格式为 18[az][az][az][0-9][0-9][0-9][0-9]

so I've tried this function所以我试过这个功能

def cutt(n):
    n=int(n[5:9])

Is there something I need to return?有什么我需要退货的吗? It shows after using the function that it has a none datatype它在使用该函数后显示它具有 none 数据类型

Thank you谢谢

You should do like this:你应该这样做:

def cutt(n):
    return int(n[5:9])

and in your code:并在您的代码中:

old_n = '18abc1234'
new_n = cutt(old_n)

the your new_n will be 1234 .你的new_n将是1234

Use the code使用代码

def cutt(n):
    n=int(n[5:9])
    return n

Checking检查

val=cutt("18xyz3456")
print(val)

Output输出

3456

Hope it helps希望能帮助到你

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

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