The following is my effort to understand how to process a string, letter, by letter:name = name[index].upper() print('if block {} and index {}'.format(name[index], index)) elif (index % 2 > 0): print(index) print('Start: elseif block, index is {}, letter is {}'.format(
def myfunc(name): index = 0 howmax = len(name) # while (index <= howmax): while (index < howmax): if (index % 2 == 0): print('letter to upper = {}, index {}!'.format(name[index], index))
return name
myfunc('capitalism')
Error message: Not making sense, index is 1, letter s/b 'a'letter to upper = c, index 0!
if block C and index 0
1
Start: elseif block, index is 1, letter is C --------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[27], line 21
17 # index = name.upper()
19 return name
21 myfunc('capitalism')
Cell In[27], line 8, in myfunc(name)
6 while (index < howmax):
7 if (index % 2 == 0):
----> 8 print('letter to upper = {}, index {}!'.format(name[index], index))
9 name = name[index].upper()
10 print('if block {} and index {}'.format(name[index], index))
IndexError: string index out of range***************************************************
So, I'm doing something... Stupid!! ***************************************************
"When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze."
Isaiah 43:2
Also, it's 2024 ... time to start using f-strings (because they are
more readable than str.format())
The following is my effort to understand how to process a
string, letter, by letter:
The following is my effort to understand how to process a string, letter, by letter:name = name[index].upper() print('if block {} and index {}'.format(name[index], index)) elif (index % 2 > 0): print(index) print('Start: elseif block, index is {}, letter is {}'.format(
def myfunc(name): index = 0 howmax = len(name) # while (index <= howmax): while (index < howmax): if (index % 2 == 0): print('letter to upper = {}, index {}!'.format(name[index], index))
return name
myfunc('capitalism')
Error message: Not making sense, index is 1, letter s/b 'a'letter to upper = c, index 0!
if block C and index 0
1
Start: elseif block, index is 1, letter is C --------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[27], line 21
17 # index = name.upper()
19 return name
21 myfunc('capitalism')
Cell In[27], line 8, in myfunc(name)
6 while (index < howmax):
7 if (index % 2 == 0):
----> 8 print('letter to upper = {}, index {}!'.format(name[index], index))
9 name = name[index].upper()
10 print('if block {} and index {}'.format(name[index], index))
IndexError: string index out of range***************************************************
So, I'm doing something... Stupid!! ***************************************************
"When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze."
Isaiah 43:2
On Wed, 29 May 2024 at 16:03, Cameron Simpson via Python-list <[email protected]> wrote:
By which Thomas means stuff like this:
print(f'if block {name[index]} and index {index}')
Notice the leading "f'". Personally I wouldn't even go that far, just:
print('if block', name[index], 'and index', index)
But there are plenty of places where f-strings are very useful.
I wouldn't replace str.format() everywhere, nor would I replace
percent encoding everywhere - but in this case, I think Thomas is
correct. Not because it's 2024 (f-strings were brought in back in
2015, so they're hardly chronologically special),
but because most of
this looks like debugging output that can take advantage of this
feature:
print(f"if block {name[index]=} {index=}")
ChrisA
On 2024-05-29, Chris Angelico via Python-list <[email protected]> wrote:
print(f"if block {name[index]=} {index=}")
Holy cow! How did I not know about the f-string {=} thing?
On 29 May 2024, at 05:38, Kevin M. Wilson via Python-list <[email protected]> wrote:
The format in this email is not of my making, should someone know, how to do this so that it's a readable script do tell!
KMW
On 5/29/2024 8:55 AM, Kevin M. Wilson wrote:
Please recall, I said the format for the email failed to retain the
proper indents.
I'll attach a picture of the code!
Purpose; to uppercase every other letter in a string.
Thanks all, KMW
Simpler is good, and readability is good. For a simple conversion that
has a little touch of generality:
s1 = 'this is a test'
def convert(i, ch):
return ch.upper() if i % 2 else ch
result = ''.join([convert(i, ch) for i, ch in enumerate(s1)])
print(result) # tHiS Is a tEsT
However, this has a weakness: what to do about spaces. Should they be counted among the characters to be uppercased? or should they not be
included in the count of the alternation? If you want to uppercase
every other letter that is not a space, things become a little more complicated. And then do you want this to apply to all whitespace or
only spaces?
If you want to skip changing spaces, then you need to track the state of converted characters in some way. It would probably be easier (and more readable) to use a "for x in t:" construction:
def convert(convert_this, ch):
"""Convert character ch if convert_this is True.
Don't convert spaces.
"""
if convert_this:
if ch == ' ':
return (convert_this, ch)
elif convert_this:
return (False, ch.upper())
return (True, ch)
convert_next = False
result = ''
for ch in s1:
convert_next, ch = convert(convert_next, ch)
result += ch
print(result) # tHiS Is A TeSt
There could be even more complications if you allow non-ascii characters
but you were asking about processing character by character so I won't
get into that.
(You haven't specified the problem in enough detail to answer questions
like those).
Please recall, I said the format for the email failed to retain the
proper indents.
I'll attach a picture of the code!
Purpose; to uppercase every other letter in a string.
Thanks all, KMW
***************************************************
"When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze." *Isaiah 43:2
*
On Wednesday, May 29, 2024 at 06:19:56 AM MDT, Thomas Passin via
Python-list <[email protected]> wrote:
On 5/29/2024 3:14 AM, Chris Angelico via Python-list wrote:
On Wed, 29 May 2024 at 16:03, Cameron Simpson via Python-list <[email protected] <mailto:[email protected]>> wrote:
By which Thomas means stuff like this:
print(f'if block {name[index]} and index {index}')
Notice the leading "f'". Personally I wouldn't even go that far, just:
print('if block', name[index], 'and index', index)
But there are plenty of places where f-strings are very useful.
I wouldn't replace str.format() everywhere, nor would I replace
percent encoding everywhere - but in this case, I think Thomas is
correct. Not because it's 2024 (f-strings were brought in back in
2015, so they're hardly chronologically special),
I only meant that they have been around for 9 years and are usually more readable, so just change over already. I had some inertia over them
myself (imagine sticking with % formatting!) so I understand.
but because most of
this looks like debugging output that can take advantage of this
feature:
print(f"if block {name[index]=} {index=}")
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list <https://mail.python.org/mailman/listinfo/python-list>
On 2024-05-29 15:32, Thomas Passin via Python-list wrote:
On 5/29/2024 8:55 AM, Kevin M. Wilson wrote:[snip]
Please recall, I said the format for the email failed to retain the
proper indents.
I'll attach a picture of the code!
Purpose; to uppercase every other letter in a string.
Thanks all, KMW
Simpler is good, and readability is good. For a simple conversion that
has a little touch of generality:
s1 = 'this is a test'
def convert(i, ch):
return ch.upper() if i % 2 else ch
result = ''.join([convert(i, ch) for i, ch in enumerate(s1)])
print(result) # tHiS Is a tEsT
Small mistake there. The original code converted to uppercase on even indexes, whereas your code does it on odd ones.
However, this has a weakness: what to do about spaces. Should they be
counted among the characters to be uppercased? or should they not be
included in the count of the alternation? If you want to uppercase
every other letter that is not a space, things become a little more
complicated. And then do you want this to apply to all whitespace or
only spaces?
If you want to skip changing spaces, then you need to track the state of
converted characters in some way. It would probably be easier (and more
readable) to use a "for x in t:" construction:
def convert(convert_this, ch):[snip]
"""Convert character ch if convert_this is True.
Don't convert spaces.
"""
if convert_this:
if ch == ' ':
return (convert_this, ch)
elif convert_this:
return (False, ch.upper())
return (True, ch)
convert_next = False
result = ''
for ch in s1:
convert_next, ch = convert(convert_next, ch)
result += ch
print(result) # tHiS Is A TeSt
There could be even more complications if you allow non-ascii characters
but you were asking about processing character by character so I won't
get into that.
(You haven't specified the problem in enough detail to answer questions
like those).
Small mistake there. The original code converted to uppercase on even >indexes, whereas your code does it on odd ones.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 47:00:57 |
| Calls: | 12,112 |
| Calls today: | 3 |
| Files: | 15,010 |
| Messages: | 6,518,497 |