Why Print function need to print anything in Python always?
sumitdba10 (0)

str1 = "Hello"
str1[0] = not executing while
print(str1[0]) = H is giving result

You are viewing a single comment. View All
lclarkejhdf (10)

Sorry if I'm accidentally repeating anything @MichelleA8 has said, or if I don't answer your question, but I'll just give some info in case it helps you in any way!

As for what you've said in the description of your question, str1[0] is just going to be "H". It's only a piece of data so it can't be executed, just as if you were to type 5 or "E" into the code, it's not going to do anything (and may cause an error); the print command is the instruction itself which can use the data and is needed to get the code to display the data in the shell.

As for what's in the code, assigning a print command, or any command, to a variable will most likely cause issues (as far as I know). If you want sum1 to equal the sum of 2 + 3, then you just need 'sum1 = 2 + 3' (without the 's) as all we're doing at this point is manipulating data and storing it somewhere with an identifier (the variable name). This will then allow 'print(sum1 + 2)' to work and display '7' in the shell (both without the 's) as sum1 now contains data the code can manipulate instead of trying to add a number to a command or instruction.

I hope this has been helpful in any way and I'm sorry again if I've accidentally stepped on @MichelleA8's toes (as it were) or worded this in a confusing way!

MichelleA8 (2)

@lclarkejhdf It's a good explanation, a bit wordy for a beginner, but a fine one. I'm a little hazy on Python since I haven't used it since April.

lclarkejhdf (10)

@MichelleA8 Yeah, sorry for that; I think it's just because I've become so used to Python that I'm not entirely sure how to explain parts of it without accidentally rambling or going too in-depth or whatever.