Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数返回python中的格式化字符串_Python_Python 2.7_String Formatting - Fatal编程技术网

函数返回python中的格式化字符串

函数返回python中的格式化字符串,python,python-2.7,string-formatting,Python,Python 2.7,String Formatting,在args(0…len(args)) 例如: simple_format("%1 calls %0 and %2", "ashok", "hari")=="hari calls ashok and %2" 请帮帮我。更新: "{1} calls {0} and {2}".format("hari", "ashok", "x") >>> 'ashok calls hari and x' 更新: "{1} calls {0} and {2}".format("hari", "a

args(0…len(args))

例如:

simple_format("%1 calls %0 and %2", "ashok", "hari")=="hari calls ashok and %2"

请帮帮我。

更新:

"{1} calls {0} and {2}".format("hari", "ashok", "x")
>>> 'ashok calls hari and x'

更新:

"{1} calls {0} and {2}".format("hari", "ashok", "x")
>>> 'ashok calls hari and x'

更新:

"{1} calls {0} and {2}".format("hari", "ashok", "x")
>>> 'ashok calls hari and x'

更新:

"{1} calls {0} and {2}".format("hari", "ashok", "x")
>>> 'ashok calls hari and x'
如果您确实需要函数
simple\u format
,则:

import re
def simple_format(*args):
    s = re.sub(r'%(\d+)', r'{\1}', args[0])
    return s.format(*args[1:])
例如:

>>> simple_format("%1 calls %0 and %2", "ashok", "hari", "tom")
'hari calls ashok and tom'
如果您确实需要函数
simple\u format
,则:

import re
def simple_format(*args):
    s = re.sub(r'%(\d+)', r'{\1}', args[0])
    return s.format(*args[1:])
例如:

>>> simple_format("%1 calls %0 and %2", "ashok", "hari", "tom")
'hari calls ashok and tom'
如果您确实需要函数
simple\u format
,则:

import re
def simple_format(*args):
    s = re.sub(r'%(\d+)', r'{\1}', args[0])
    return s.format(*args[1:])
例如:

>>> simple_format("%1 calls %0 and %2", "ashok", "hari", "tom")
'hari calls ashok and tom'
如果您确实需要函数
simple\u format
,则:

import re
def simple_format(*args):
    s = re.sub(r'%(\d+)', r'{\1}', args[0])
    return s.format(*args[1:])
例如:

>>> simple_format("%1 calls %0 and %2", "ashok", "hari", "tom")
'hari calls ashok and tom'

下面是一个使用
string.Template
的示例:

from string import Template

def simple_format(text, *args):
    class T(Template):
        delimiter = '%'
        idpattern = '\d+'
    return T(text).safe_substitute({str(i):v for i, v in enumerate(args)})

simple_format("%1 calls %0 and %2", "ashok", "hari")
# hari calls ashok and %2

下面是一个使用
string.Template
的示例:

from string import Template

def simple_format(text, *args):
    class T(Template):
        delimiter = '%'
        idpattern = '\d+'
    return T(text).safe_substitute({str(i):v for i, v in enumerate(args)})

simple_format("%1 calls %0 and %2", "ashok", "hari")
# hari calls ashok and %2

下面是一个使用
string.Template
的示例:

from string import Template

def simple_format(text, *args):
    class T(Template):
        delimiter = '%'
        idpattern = '\d+'
    return T(text).safe_substitute({str(i):v for i, v in enumerate(args)})

simple_format("%1 calls %0 and %2", "ashok", "hari")
# hari calls ashok and %2

下面是一个使用
string.Template
的示例:

from string import Template

def simple_format(text, *args):
    class T(Template):
        delimiter = '%'
        idpattern = '\d+'
    return T(text).safe_substitute({str(i):v for i, v in enumerate(args)})

simple_format("%1 calls %0 and %2", "ashok", "hari")
# hari calls ashok and %2

函数以返回python格式的格式化字符串:

def simple_format(format, *args):
"""
Returns a formatted string by replacing all instances of %X with Xth argument in args (0...len(args))
e.g. "%0 says hello", "ted" should return "ted says hello"
"%1 says hello to %0", ("ted", "jack") should return jack says hello to ted etc.
If %X is used and X > len(args) it is returned as is.
"""
pass
count = 0
for name in args:
    format = format.replace("%" + str(count), name)
    count = count + 1
return format

函数以返回python格式的格式化字符串:

def simple_format(format, *args):
"""
Returns a formatted string by replacing all instances of %X with Xth argument in args (0...len(args))
e.g. "%0 says hello", "ted" should return "ted says hello"
"%1 says hello to %0", ("ted", "jack") should return jack says hello to ted etc.
If %X is used and X > len(args) it is returned as is.
"""
pass
count = 0
for name in args:
    format = format.replace("%" + str(count), name)
    count = count + 1
return format

函数以返回python格式的格式化字符串:

def simple_format(format, *args):
"""
Returns a formatted string by replacing all instances of %X with Xth argument in args (0...len(args))
e.g. "%0 says hello", "ted" should return "ted says hello"
"%1 says hello to %0", ("ted", "jack") should return jack says hello to ted etc.
If %X is used and X > len(args) it is returned as is.
"""
pass
count = 0
for name in args:
    format = format.replace("%" + str(count), name)
    count = count + 1
return format

函数以返回python格式的格式化字符串:

def simple_format(format, *args):
"""
Returns a formatted string by replacing all instances of %X with Xth argument in args (0...len(args))
e.g. "%0 says hello", "ted" should return "ted says hello"
"%1 says hello to %0", ("ted", "jack") should return jack says hello to ted etc.
If %X is used and X > len(args) it is returned as is.
"""
pass
count = 0
for name in args:
    format = format.replace("%" + str(count), name)
    count = count + 1
return format
可能的重复可能的重复可能的重复可能的重复