0

Question on Functions

I came across the hashtag generator challenge. I had to look up the answer because I wasn’t sure: s = input() def hashtagGen(text): s1 = “#” + s.replace(“ “, “”) return s1 print(hashtagGen(s)) I initially had the # part and replace function in separate lines, but the result only showed the # with the input. I figured that was because of the return line. I guess I’m a bit confused on how s1 is worked back into s? Trying to understand as much as I can before moving onto the next lessons. Thx!

22nd May 2021, 5:21 AM
Cody Benke
5 RĂ©ponses
+ 3
Visph, Sorry, I was rushing up I just forgot the '#' LOL Thanks
22nd May 2021, 5:45 AM
Ipang
+ 2
String variable <s> is passed to `hashtagGen` function through parameter <text>. So you need to remove spaces from <text> rather than <s> inside `hashtagGen`. return "#" + text.replace( " ", "" ) # <- like this
22nd May 2021, 5:34 AM
Ipang
+ 2
Visph, I just noticed that the OP also use non regular double quotes. I suppose it could also prevent the code from running correctly ...
22nd May 2021, 5:40 AM
Ipang
+ 1
you define a function argument 'text', but you use the global variable 's'... even if in this case it works as expected, it would not work if you pass another value / variable by calling the function ^^ .replace() method doesn't modify the string on wich it is applied, but rather return a new string (as string are imutable). you could do it in two lines, but by assigning the return value of .replace() to either the same variable (if it is local to the function -- if it's a goibal variable you can only change its value by declaribg it explicitky global) or another one ;) h = "#" text = text.replace(" ","") return h+text ... or in only one line as suggested by Ipang ;P
22nd May 2021, 5:36 AM
visph
visph - avatar
+ 1
Ipang sure, if that's not the regular one (cannot see that on my device and my old eyes ;P) anyway, maybe could you edit your first post to add the hastag before the replace() call ;)
22nd May 2021, 5:43 AM
visph
visph - avatar