This code works:
mystr = "hello"
for ch in mystr:
print(ch, end="")
result is: hello
Note that the for loop does not use range. Strings are iterable, that is they
support Python's iteration protocol. So, for ch in mystr: assigns one
character from mystr to ch each time, each iteration gets the next character.
To prevent each character from appearing on a separate line (in Python 3) you
need end="". That is, don't put in the usual end-of-line ending.
--- Joseph S.
Teledyne Confidential; Commercially Sensitive Business Data
-----Original Message-----
From: Michael F. Stemper <[email protected]>
Sent: Monday, February 8, 2021 9:19 AM
To: [email protected]
Subject: Re: Python cannot count apparently
On 07/02/2021 13.34, Philipp Daher wrote:
> Hello,
>
> I recently coded this snippet of code:
> myString=„hello“
> for i in range(len(myString):
> print(string[i])
>
> And now for the weird part:
>
> SOMETIMES, the output is this:
>
> hello
Strange. When I fix the errors in what you posted:
- wrong character to start the string
- wrong variable name in the call to print()
I get[1]:
... myString="hello"
... for i in range(len(myString)):
... print( myString[i] )
...
h
e
l
l
o
...
You must have done something to suppress the newlines after each call to
print().
So it's quite obvious that the code you posted has very little to do with the
code you ran. If you really want us to help, please directly copy and paste the
exact code that you ran. We can't really help you with only guesses as to what
you did.
[1] (leading > replaced with . to fool news clients)
--
Michael F. Stemper
Galatians 3:28
--
https://mail.python.org/mailman/listinfo/python-list