-1

I am trying to take a string, check every single character in the string, and swap the case for the opposite. For example, 'Seoul' would become 'sEOUL'. (I realize that Python's builtin str.swapcase method exists, but am trying to reimplement it as a learning exercise.)

This is my code.

str = "My name is Shah, and I recently went to Mission Nightclub"
str_arr = [str.upper[0] + str.upper[1:] if str[index].islower() else str[index].lower for (index, val) in enumerate(str.split)]
print(str_arr)

The idea is that I use enumerate and a list comprehension. First, it takes the whole string and splits it into a list where each element is a word. Then, it'll use enumerate to get the index of each element in the list. Then I have two conditions: one checks if the ith character is lowercase, and if it is, it'll convert it to uppercase; else it'll convert to lowercase. Then, for each ith element in the new list created, it'll take the first element and uppercase it and the rest of the element using string slicing to get the rest of the characters in the word.

However, I get this error:

Traceback (most recent call last):
  File "/Users/shah/test.py", line 2, in <module>
    str_arr = [str.upper[0] + str.upper[1:]if str[index].islower() else str[index].lower for (index, val) in enumerate(str.split)]
TypeError: 'builtin_function_or_method' object is not iterable

Why doesn't my code work, and how do I fix it?

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
Shah Jacob
  • 137
  • 1
  • 9
  • 1
    Have you tried breaking this down into smaller problems and checking that each part does what you think it does? For instance, have you tried just seeing what `str.upper[0]` is? (Also, it's not a good practice to "shadow" existing keywords and builtins, so `str` is not a good choice for a variable name.) – CrazyChucky Nov 29 '21 at 00:56
  • 1
    You. need to use `str.upper()`, not`str.upper`. It's a function. Likewise for `str.lower() `. – Frank Yellin Nov 29 '21 at 01:04
  • 1
    One Python rule of thumb: if you can't figure out why your list comprehension isn't working, write it as a for loop first. It's easier to debug, particularly by printing intermediate values as you create them in each iteration. – CrazyChucky Nov 29 '21 at 01:10
  • @CrazyChucky given ther amount of work i put in and did my best to explain everything can you please how to fix it and what i am doing wrong – Shah Jacob Nov 29 '21 at 01:13
  • 1
    @Shah The error message should tell you why it doesn't work: `TypeError: 'builtin_function_or_method' object is not iterable`. If you're not sure why that's happening, take CrazyChucky's advice: rewrite the comp as a for-loop and break it down step-by step. There are multiple problems with your code, so do them one at a time. We're not just going to debug everything for you. – wjandrea Nov 29 '21 at 01:21
  • 1
    Does this answer your question? [script to swap case in python](https://stackoverflow.com/questions/61507142/script-to-swap-case-in-python) – bad_coder Nov 29 '21 at 01:26

1 Answers1

2

Python provides a built-in method for doing this:

>>> a = 'Seoul'
>>> print(a.swapcase())
sEOUL
Pedro Maia
  • 2,666
  • 1
  • 5
  • 20