简体   繁体   English

为什么这个 function 没有返回 output 并且只使用打印?

[英]Why is this function returning no output and only working with print?

def get_formatted_text(first,last):
  full_name=(f'{first} {last}').title()
  return full_name #only works with print. Why?

get_formatted_text('Dick','Long')

I don't see any output in my terminal when I do the function call.当我调用 function 时,我在终端中看不到任何 output 。 It works with print function, but I don't understand why print is needed when return will show the output?它适用于打印 function,但我不明白为什么当返回显示 output 时需要打印?

Return will not print the value. Return 不会打印该值。 It only ' returns ' it.它只会' returns '它。 When coding directly in the Python terminal it will print the value, but that is the extra capabilities of the terminal, not the return .当直接在 Python 终端中编码时,它将打印该值,但这是终端的额外功能,而不是return In all other instances, print is needed to output to the terminal.在所有其他情况下,需要将 output print到终端。

its just RETURNING the value its up to u what u do with the value and the the brackets r also not required if u r returning the value它只是返回值它取决于你如何处理该值和括号 r 如果你返回值 r 也不需要

def get_formatted_text(first, last):
    full_name = f'{first} {last}'.title()
    return full_name  # only works with print. Why?


x = get_formatted_text('Dick', 'Long')
print(x)

But if u r returning a value u can even manuplitate like但是如果你 r 返回一个值,你甚至可以像这样操作

def get_formatted_text(first, last):
     full_name = f'{first} {last}'.title()
     return full_name  # only works with print. Why?


 x = get_formatted_text('Dick', 'Long')
 print(x.lower())

Your return statement returns the title case string.您的return语句返回标题大小写字符串。 It does not print the element in the console.它不会在控制台中打印元素。

After getting the formatted text you may update it as necessary.获得格式化文本后,您可以根据需要对其进行更新。

def get_formatted_text(first,last):
  full_name=(f'{first} {last}').title()
  return full_name

formatted_text = get_formatted_text('john','doe')
print(f"Formatted text with title case: {formatted_text}")

Output: Output:

Formatted text with title case: John Doe

Reference:参考:

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

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