猿问

Python:打印函数文本的自定义打印函数

Python脚本:


text = "abcde"

print("text[::-1] : ", text[::-1])

print("text[5:0:-1] : ", text[5:0:-1])

输出:


text[::-1] :  edcba

text[5:0:-1] :  edcb

可以定义一个自定义函数来避免输入重复吗?例如:


text = "abcde"

def fuc(x):

    print(x, ":", x)

    

fuc(text[::-1])

fuc(text[5:0:-1])

要求。输出:


text[::-1] :  edcba

text[5:0:-1] :  edcb


眼眸繁星
浏览 97回答 2
2回答

慕雪6442864

在 python 3.8+ 中,您可以使用自记录表达式>>> print(f"{a[::-1]=}") a[::-1]='edcba'

慕哥6287543

使用 f 字符串可以实现涉及字符串插值的解决方案。但是,f 字符串仅适用于 Python 3.8+但是,我们可以很容易地实现字符串插值,如如何在 Python 中实现字符串插值Current Modification 扩展了上述参考,以允许除了变量查找之外的表达式。import sysfrom re import subdef interp(s):  '''Implement simple string interpolation to handle    "{var}" replaces var with its value from locals    "{var=}" replaces var= with var=value, where value is the value of var from locals    "{expressions} use eval to evaluate expressions (make eval safe by only allowing items from local functions and variables in expression'''  # 1. Implement self-documenting expressions similar to https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging  s1 = sub( r'{\s*(.*?)=\s*}', lambda m: m.group(1) + '=' + '{' + m.group(1) + '}', s)  # Get the locals from the previous frame  previous_frame = sys._getframe(1)  # i.e. current frame(0), previous is frame(1)  d = previous_frame.f_locals        # get locals from previous frame  # 2--Replace variable and expression with values  # Use technique from http://lybniz2.sourceforge.net/safeeval.html to limit eval to make it safe  # by only allowing locals from d and no globals  s2 = sub(r'{\s*([^\s]+)\s*}', lambda m: str(d[m.group(1)]) if m.group(1) in d else str(eval(m.group(1), {}, d)), s1)  return s2# Testa = "abcde"print(interp("a has value {a}"))  # without self-doc =#Output>>> a has value abcdeprint(interp("{a[::-1]=}"))       # with self-doc =#Output>>> a[::-1]=edcbaprint(interp('{a[4:0:-1]=}'))     # with self-doc =#Output>>> a[4:0:-1]=edcbprint(interp('sum {1+1}') # without self-doc =#Output>>> sum 2print(interp('{1+1=}'))  # with self-doc =#Output>>> 1+1=2
随时随地看视频慕课网APP

相关分类

Python
我要回答