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

kederrac
  • 16,819
  • 6
  • 32
  • 55
Walls
  • 149
  • 13
  • [Python strings are immutable](https://stackoverflow.com/questions/9097994/arent-python-strings-immutable-then-why-does-a-b-work), if that's what you're worried about. – pault Feb 06 '20 at 18:03

3 Answers3

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

No need to create a new variable.

Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
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.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
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.