使用自定义转换类型扩展字符串格式 [英] Extending string formatting with custom conversion types

查看:31
本文介绍了使用自定义转换类型扩展字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string.Formatter 允许扩展新的样式格式使用自定义转化类型.

string.Formatter allows the extension of new style formatting with custom conversion types.

这也适用于旧的 "%" 样式的格式字符串吗?有图书馆吗?

Is this possible for the older "%"-style formatting strings too? Is there a library for this?

推荐答案

也许

class CustomFormat(object):
    def __init__(self, obj):
        self.Object = obj

    def __str__(self):
        return str(self.Object).upper()  # your magic goes here...

print "abc%s" % customFormat("hello")
# abcHELLO

这篇关于使用自定义转换类型扩展字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆