1

I'm new to python programming, and as a beginner I want to start by using a code editor, I choose sublime text 4 but I face this problem, So help me please ! This is the code :

def return_string(your_string):
    if len(your_string) >= 4:
        new_string = your_string[0:2] + your_string[-2:]
        return new_string
    elif len(your_string) == 2:
        new_string = your_string * 2
        return new_string
    elif len(your_string) < 2:
        new_string = ""
        return new_string

return_string("welcome")**

the expected output is "weme" but I get nothing in sublime text output (when I click Ctrl + B).

When I change return to print the code is executed properly.

problem with no problem with

By the way the code above works in vscode without any problem.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264

2 Answers2

1

Python doesn't print outside the REPL normally, unless you explicitly tell it to. Add a call to print on your last line:

print(return_string('welcome'))

This is why adding an explicit print in your function works.

You can store the value into a variable first if you want to use it elsewhere:

result = return_string('welcome')
print(result)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Please I need some clarification, why the code is executed properly in vscode ide using return statement and just calling the function without print it just by typing _______return_string("welcome")_______ – The_Inquisitive Mar 30 '22 at 11:10
  • 1
    @The_Inquisitive. VS code likely outputs the result of the last statement when you run a script – Mad Physicist Mar 30 '22 at 14:01
0

Because "return string" returns a string, you must first save the data in a variable, which you may then use later in the program.

result_string = return_string("welcome")
print(result_string)