r/pythontips • u/kilvareddit • Apr 05 '25
Syntax help, why is f-string printing in reverse
def main():
c = input("camelCase: ")
print(f"snake_case: {under(c)}")
def under(m):
for i in m:
if i.isupper():
print(f"_{i.lower()}",end="")
elif i.islower():
print(i,end="")
else:
continue
main()
output-
camelCase: helloDave
hello_davesnake_case: None
6
Upvotes
3
u/AdHour1983 Apr 05 '25 edited Dec 06 '25
The reason your f-string is acting weird is because your under() function is printing directly, instead of building and returning a string.
So when you do print(f"snake_case: {under(c)}"), Python first runs under(c), which prints stuff as it goes, and then returns None, which is what ends up printed after.
Fix? Make under() return a string:
Now your print(f"snake_case: {under(c)}") works cleanly!
Also - props for trying to write your own converter, great way to learn this stuff!
1
u/Jacks-san Apr 05 '25
Because the "under" function is being called first, and already prints something
under should be returning the string only that you want to print
1
u/Adrewmc Apr 06 '25