Why cant I use a variable in str slicing? - python

So I answered one of the questions on Coding Bat that asked to return half of a string n number of times by defining a variable that takes half the length of the original string and using that variable in slicing the string.
Coding bat accepted the result, but when I try to recreate the problem on Jupyter Notebook it only accepts "integers or None or index method".
What am I doing wrong?
Im a beginner in Python and just want to make sure that I'm learning the correct format.
def first_half(str):
newword = len(str) / 2
return '{}'.format(str[:newword])
for first_half('Milkshakes') I expect to get 'Milk'.
However, the error im getting is:
TypeError: slice indices must be integers or None or have an __index__ method

This is because len(str) / 2 gives you a float value 5.0 and you cannot use a float as a argument to string slice, convert the argument to an int by doing int(len(str) / 2), which gives you 5, and it should work. Note that the above only holds true for Python 3 the original code you have still works for Python 2 where you don't need the float to int conversion
Also str is a python builtin, so it's bad practice to use it as a variable.
In addition, you want the first half of Milkshakes is Milks, and not Milk
def first_half(s):
#Parse float to int
newword = int(len(s) / 2)
return '{}'.format(s[:newword])
print(first_half('Milkshakes'))
#Milks
To make a generic solution, as some of the commentors have suggested, you can use integer division //, which works for both Python 2 and Python 3
def first_half(s):
#Integer division
newword = len(s) // 2
return '{}'.format(s[:newword])
print(first_half('Milkshakes'))

Related

TypeError: object of type 'int' has no len() *subtraction*

I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4
number_1_raw = random.randrange(1, 1000)
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)
I wanted the script to give me an answer that ranged from 1-3, however the
code isn't executing properly.
The desired output would be something like this:
0617 or 0071 or 0008, etc.
Please help with this issue, thanks!
You cant call len on an integer.
The len() function returns the number of items in an object.
When the object is a string, the len() function returns the number of
characters in the string.
so what you can do is
number_1_len = len(str(number_1_raw))
str changes int type to string type.
This line number_1_len = len(number_1_raw)  is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want
I think you need:
import random
number_1_raw = random.randrange(1, 1000)
number_1_len = len(str(number_1_raw))
number_of_zeros = 4 - number_1_len
print(number_of_zeros)
result = "0" * number_of_zeros
print(result+str(number_1_raw))
Output
1 # number of zeros
0801
What's wrong with your code
You are trying to find length of an integer number_1_raw which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len on it.
Furthermore, in print statement you need to convert number_1_raw into string so that it can be appended. You can not perform + operation on str and int.
I added the str() in a specific line so that your code runs without any error
import random
number_1_raw = str(random.randrange(1, 1000)) #I added the str() in this line
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)
it is completely your code except that modified line bro.
You had coded well bro but a small datatype mistake only you can rectify them as you learn.
i hope this is useful :)
First of all, I need to understand better what is happening...
Fist
number_1_raw = random.randrange(1, 1000)
The random.randrange returns a number, probably int.
Second
number_1_len = len(number_1_raw)
Has a problem, because number_1_raw is an int and len is a function that counts the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...
Is important you show all which error you're getting... and explain better what you're trying to do...

Using variables as slice indices for python

For a class I am learning how to slice integers. In the code below the variable halflength is equal to half of the length of the variable message.
new = message[halflength::]
halflength is equal to an integer, however whenever I run this code I get this error:
TypeError: slice indices must be integers or None or have an __index__ method
Basically I need to try and create a new string that is equal to the second half of the original string.
Example: original string 1234 would produce 34 as the new string.
I think the problem is you get a float type for halfLength after division, try to cast it to int, or use integer division
halfLength = int(halfLength)
or
halfLength = len(message) // 2
to do what you want to do try something like this:
halfLength=len(message)//2
newMessage=message[halfLength::]
if you get the length this way it will always be an integer that you can then use to get parts of stings with.
Make sure halflength is of type Integer.
You can use "isinstance" method to verify.
# python
Python 2.7.5 (default, Aug 2 2016, 04:20:16
>>> halflength = "4"
>>> isinstance(halflength,int)`
False`
>>> halflength=4
>>> isinstance(halflength,int)
True
>>>
Try This:
message[int(halflength)::]

How would you unpack a 32bit int in Python?

I'm fairly weak with structs but I have a feeling they're the best way to do this. I have a large string of binary data and need to pull 32 of those chars, starting at a specific index, and store them as an int. What is the best way to do this?
Since I need to start at an initial position I have been playing with struct.unpack_from(). Based on the format table here, I thought the 'i' formatting being 4 bytes is exactly what I needed but the code below executes and prints "(825307441,)" where I was expecting either the binary, decimal or hex form. Can anyone explain to me what 825307441 represents?
Also is there a method of extracting the data in a similar fashion but returning it in a list instead of a tuple? Thank you
st = "1111111111111111111111111111111"
test = struct.unpack_from('i',st,0)
print test
Just use int
>>> st = "1111111111111111111111111111111"
>>> int(st,2)
2147483647
>>> int(st[1:4],2)
7
You can slice the string any way you want to get the indices you desire. Passing 2 to int tells int that you are passing it a string in binary

Python - String of Digits to Integer using Recursion?

Say I have a string = '123' but I want to convert it into 123 without using the int() function. How would I go upon doing this using recursion?
The idea I have so far is to put the string into an array as such ['1','2','3'] and then convert them based on ASCII representations.
for i in string:
myArr.append (ord(i) - ord('0'))
So now I'd have a list looking like [1,2,3]. What should I do next to recursively get 123?
I have an idea of using the place values and add them together (ie. 100 + 20 + 3 = 123), but I don't know how to do this. Any ideas would be helpful !
I guess this is an academic exercise, because it's a rather contrived thing to ask. Here's how, assuming that s represents an integer number greater than or equal to zero:
def strToInt(s, acc):
if not s:
return acc
return strToInt(s[1:], 10 * acc + ord(s[0]) - 48)
Or even shorter:
def strToInt(s, acc):
return strToInt(s[1:], 10 * acc + ord(s[0]) - 48) if s else acc
The trick is to accumulate the result of the conversion in an extra parameter, this has the nice side effect of yielding a tail-recursive solution (not that it matters much in Python, but still…). Also notice how we use ord() to get the numeric value of a character representing a digit, and by simply subtracting 48 we get to the actual digit's value. Test it, it works as expected:
strToInt('123', 0) # the accumulator always starts in zero
=> 123
This wouldn't be recursive (I don't think you understand what that means), but:
for char in string:
array.append(ord(char) - ord('0'))
array.reverse()
num = 0
for index, val in enumerate(array):
num += val * (10 ** index)
EDIT: Ah, I see this is for an exercise. Yeah, it's a ridiculous problem to solve via recursion, but one of the other answers does it.
Another possibility:
def strToInt(s):
if s:
return (ord(s[-1]) - ord('0')) + 10 * strToInt(s[:-1])
else:
return 0
The first thing that comes to mind is the famous C method atoi().
Python had a similar method, also called atoi(), but it was deprecated, with the instruction to use int().
Therefore, despite your comment, I would recommend using int(). Python documentation encourages you to use it, so do so without fear.
int() documentation for Python 2.7
int() documentation for Python 3.3

Integer to binary [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert an integer to binary without using the built-in bin function
This function receives as a parameter a number in base 10 and should return a list representing the same value expressed in binary as a list of bits, where the first element in the list is the most significant (leftmost) bit.
convert_10_to_2(11) should return [1, 0, 1, 1]
I cannot use the binary function or outside functions, so it has to be done in a more complicated way.
b = ''
while num > 0:
b = str(num % 2) + b
num >>= 1
return (b)
Okay I got my code finally up, okay I get '1011', but I need [1,0,1,1], I can't really use any functions such as bin(x) or binary_list. That has been what's taking this question so long.
You can initialize a list, then iterate through the string using a for loop and append the value to the list every iteration, like such.
binary_string = convert_10_to_2(11)
binary_list = []
for character in binary_string:
binary_list.append(int(character))
bin(X), x is the integer the function returns a binary string
MORE # python build in functions
This will work for both Python 2.x and 3.x:
list(map(int, '{:b}'.format(11)))
Replace 11 with other number, if you wish. Remove enclosing list call, if you do not need support for Python 3.x. Enclose it in function, if you really find it necessary.

Categories