Can some please help me identify where the error is? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can some please help me identify where the error is?

I am busy with basic python exercises here. I keep getting a TypeError: 'type' object is not subscriptable message. s = ("Enter a string: ") print("Original String is", s) for i in range(0, len(s)-1, 2): print("Printing only even index chars") print("index[",i,"]", str[i] ) 👆 That is my own program. _________________________________________________ 👇 This is the solution from some website I got the exercise from. def printEveIndexChar(str): for i in range(0, len(str)-1, 2): print("index[",i,"]", str[i] ) inputStr = input("Enter String ") print("Orginal String is ", inputStr) print("Printing only even index chars") printEveIndexChar(inputStr)

21st Apr 2020, 12:14 PM
Butcher
Butcher - avatar
4 Answers
+ 1
So let's start by analyzing the error here: 'type' object is not subscriptable. This may sound a little vague so we'll dissect it: 'type' object: any built-in type really, so: int, float, str, list, tuple, ... subscriptable: a subscriptable is something you access an item of; a container, like so: list_a[1], some_tuple[1:3] The a[i] notation is called subscripting So, what must've happened is that you tried to access an item of a type, which doesn't make sense. And you did: str[i] on line 6. Also, your string is originally named s, so you might want to write s[i] on line 6, instead. A rule of thumb is to never name your variables the same as built-ins like: str, float, int, input, max, min, sum, iter, next, etc. Because then you'll lose the reference to that built-in and you can't use it in later code. Huge problem if other people import your code into their own. You can see a list of such names by doing: import builtins print(dir(builtins)) Have fun pythoning!
21st Apr 2020, 5:01 PM
Zuke
Zuke - avatar
+ 1
P.S. have a look at f-strings: when you prepend a literal string (not a variable name) with an unquoted f, you get an f-string. They help you "format" your code properly. Say you wanna print a variable's content inside a string. One way is to print('Hello, I\'m ' + name + '. Nice to meet you.'), or print('Hello, I\'m', name, '. Nice to meet you.'). But you can also put your variable inside { } in an f-string like this: print(f'Hello, my name is {name}. Nice to meet you.'). That saves you and your readers some undeserved headache. Python supports expressions inside { } so you can do: print(f'my favourite list is {[i**2 for i in range(10)]}'). No necessity to print it by the way. You can hold it in a variable: h = 195 # int w = '83' # str Me = f'height: {h}cm, weight: {w}kg' # Me: 'height: 195cm, weight: 83kg'
21st Apr 2020, 5:26 PM
Zuke
Zuke - avatar
+ 1
f-strings (cont'd) Now, format specifiers! I didn't really find a good resource for this online so here's how far trial and error gets you: >>> pi = 3.141 >>> f'{pi: ^8}' # center leaning-left of total length 8 ' 3.141 ' >>> f'{pi: <8}' # align left '3.141 ' >>> f'{pi: >8}' # align right ' 3.141' >>> f'{pi:x^15}' # specify fill character 'xxxxx3.141xxxxx' >>> f'{pi:.2f}' # truncate float '3.14' >>> f'{pi:?>20.1f}' # all together '?????????????????3.1'
21st Apr 2020, 5:27 PM
Zuke
Zuke - avatar
+ 1
Zuke Thanks. 🙏 That was helpful. I got the code to run. Stay blessed
21st Apr 2020, 5:37 PM
Butcher
Butcher - avatar