2

I am trying to make a function to duplicate a string from one variable and returned it with two arguments, what im trying to do is if I type pie then it will return ('pie','pie'), here is what i already do :

def duplicateVariabel(kata):
  kata_dua = kata
  return kata,kata_dua
print(duplicateVariabel("aku"))

so basically I'm trying to find another way to duplicate aku without using kata_dua = kata but still return 2 variables

1

3 Answers 3

4
>>> kata = "kata"
>>> (kata, kata)
('kata', 'kata')

No need to create a new variable.

Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

def duplicateVariabel(kata):
   return kata,kata
print(duplicateVariabel("aku"))

duplicateVariabel returns the tuple containing the string values.

so basically im trying to find another way to duplicate aku without using kata_dua = kata but still return 2 variables

Note: Strings in Python are immutabe and variables holds references (they are similar to pointers) to the values/object. So, you aren't basically duplicating the strings. See this.

See this for the difference between shallow and deep copy.

kata_dua = kata

The above statement doesn't do shallow or deep copy. It only copies the reference stored in variable kata to kata_dua variable. After the above statement, they both point to the same object (for example, "aku")

If you don't believe me, try this:

abcd = "abhi"
efgh = abcd
print("ABCD is ", id(abcd))
print("EFGH is ", id(efgh))

They both print the same value.

Comments

2

you can simply write return kata, kata

just keep in mind, when you return to comma separated variable it returns a tuple. So, access the content accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.