Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to create a string array in python?

I'm relatively new to Python and it's libraries and I was wondering how I might create a string array with a preset size. It's easy in java but I was wondering how I might do this in python.

So far all I can think of is

strs = ['']*size 

And some how when I try to call string methods on it, the debugger gives me an error X operation does not exist in object tuple.

And if it was in java this is what I would want to do.

String[] ar = new String[size]; Arrays.fill(ar,""); 

Please help.

Error code

    strs[sum-1] = strs[sum-1].strip('\(\)') AttributeError: 'tuple' object has no attribute 'strip' 

Question: How might I do what I can normally do in Java in Python while still keeping the code clean.

like image 388
if_zero_equals_one Avatar asked Jun 16 '11 18:06

if_zero_equals_one


People also ask

How do you create an empty string array in Python?

Python numpy empty string array To create an empty array of strings we can easily use the function np. empty(). To create an empty array of 4 strings (with size 3), we need to pass 'S3' as a dtype argument in the np. empty() function.

Can array store string in Python?

check the docs - you could use array to hold characters (one string basically) but it is not intended to hold strings.


1 Answers

In python, you wouldn't normally do what you are trying to do. But, the below code will do it:

strs = ["" for x in range(size)] 
like image 129
Steve Prentice Avatar answered Oct 09 '22 18:10

Steve Prentice