I want to give a space along with value - python

I have a question about python string.
I'm trying to make a wx.listbox, and I want to input 2 items.
In this case can't I give a space between 2 string and integer?
for item in test():
self.Append('%s %d'%(str(item.name), item.cnt))
I want to give a gap between %s and %d using variable.

The space is there, maybe just not quite visible, you may need something like str.ljust to make the appended strings aligned. E.g.:
In [14]: for i, s in enumerate(['name', 'looongname']):
...: print '%s%d'%(s.ljust(20), i)
#prints out:
name 0
looongname 1

When you want to join two strings just use the join-function. There you can choose what to use between the strings:
' '.join([(str(item.name), item.cnt])
You can put everything between the quotes.

Related

Is it possible to hard declare a variable in Python?

I am trying to use a variable inside a substructure. I guess the variable should be of integer data type, and I am trying to add a loop here but it my data type is list since it contains multiple integers.
INV_match_id = [['3749052'],['3749522']]
from statsbombpy import sb
for x in range(2):
match=INV_match_id[x]
match_db = sb.events(match_id=match)
print(match)
I have tried to extract the data one by one using another variable, but still it got declared as list. Whenever I give direct values to "match" it works. for eg: if I add a line match=12546 the substructure takes the value properly.
Next thing I want to try is hard declare "match" variable as integer. Any input is appreciated. I am pretty new to Python.
Edit: Adding this solution from #quamrana here.
"So, to answer your original question: Is it possible to hard declare a variable in Python?, the answer is No. Variables in python are just references to objects. Objects can be of whatever type they want to be."
You said: " I want to loop and take the numbers one by one."
Did you mean this:
for match in INV_match_id:
match_db = sb.events(match_id=match)
I don't know what you want to do with match_db
Update:
"that single number is also declared as a list. like this- ['125364']"
Well if match == ['125364'] then it depends on whether you want: "125364" or 125364. I assume the latter since you talk a lot about integers:
for match in INV_match_id:
match = int(match[0])
match_db = sb.events(match_id=match)
Next Update:
So you have: INV_match_id = ['3749052','3749522']
This means that the list is a list of strings, so the code changes to this:
for match in INV_match_id:
match_db = sb.events(match_id=int(match))
Your original code was making match into a list of the digits of each number. (eg match = [1,2,5,3,6,4])
Reversionary Update:
This time we have: INV_match_id = [['3749052'],['3749522']]
that just means going back to the second version of my code above:
for match in INV_match_id:
match = int(match[0])
match_db = sb.events(match_id=match)
It's as simple as:
from statsbombpy import sb
INV_match_id = [['3749052'],['3749522']]
for e in INV_match_id:
match_db = sb.events(match_id=e[0])
print(match_db)
You have a list of lists albeit that the sub-lists only contain one item.
match_id can be either a string or int

How do you print an entire row of a list made of words?

I have simplified my question where I just have a 2x2 list of words. I'm trying to print an entire row but it evidently doesn't work the same as with a list of numbers. How would I print an entire row?
This is for simple list manipulation.
array=[dog, cat],[hat,cap]
print(array[0][:])
I'm looking for an output of dog cat.
I assume you mean
array = [['dog','cat'],['hat','cap']]
and you want to print without the quotation marks and square brackets. In this case
print(' '.join(array[0]))
should do the job. Note the space in quotation marks.
Generally, its advised that you don't name your variables (in this case 'array') after things that are already part of python. Might be better to choose something like
string_list = [['dog','cat'],['hat','cap']]
array = [['dog','cat'],['hat','cap']]
a = list(array)
print(a[0:1])
Maybe this will help? I'm beginner in python so I'am not sure if this solution is right. After this it will print: [['dog', 'cat']]

Add a character to a string in multiple positions in Python 3

Python beginner here, sorry if this is a dumb question.
So I have a long string, and I need to add a character in very specific areas of the strings. For example, a | after character number 23, 912, and 1200. I read this Add string in a certain position in Python, but it only works for adding one character.
Also, the solution needs to be expandable, not just do it 3 times. The code I'm making can have lots of different locations with where I want the character to be.
With reference to the link that you posted Add string in a certain position in Python;
If you would like to repeat the operation for different values, you could create a list containing all index positions where you would like your | character to be inserted.
For example,
>>> l = [1, 3, 4]
>>> s = "abcdef"
>>> for i in l:
>>> s = s[:i] + "|" + s[i:] # as suggested in your link
>>> s
'a|b||cdef'
This will allow you to repeat the process for the set of values that you provide in the list. You could also define a function to assist in this, which I could explain if this method is insufficient!
Note, however, that this will insert the character relative to the current iteration. That is, in this example, after adding the | at position 1, the next insert position, 3, is different from what it was before the first insert. You could avoid this (if you want) by including a counter variable to offset all the index positions by the number of inserts that have been executed (will require initial list to be ordered).
Not so good at python, hope I can help
According to that site you went to, you can make a while loop to solve the problem
The code should look something like this
def insert_dash(string, index, addin):
return string[:index] + addin + string[index:]
alldone = False
string = input("String: ")
index = " "
while index:
index = input("Index: ")
addin = input("Add into: ")
string = insert_dash(string, index, addin)
Hope it helps!
PS: I have NOT tried the code, but I think it will work

replace '0xNN' with a byte in a string in python

I have string say
string1 = '0x000x200x300x00'
I want result like
result = '\x00\x20...'
I want to replace every '0x' with '\x' . How to do it ?
I have tried replace function in python like as follows
y = x.replace('0x', '\x')
y = x.replace('0x', '\\x')
y = x.replace('0x', r'\x')
But no success :( .
Can anyone help in this issue ?
string1.replace("0x","\\x") use `\\` to escape the `\`
/ is also not the same as\.
string1 = '0x000x200x300x00'
print string1.replace("0x","\\x")
\x00\x20\x30\x00
In [19]: string1 = '0x000x200x300x00'
In [20]: print string1.replace("0x","\\x") # str
\x00\x20\x30\x00
In [21]: string1.replace("0x","\\x") # repr
Out[21]: '\\x00\\x20\\x30\\x00'
What exactly are your trying to accomplish?
I am a bit wary to post this answer because it does not directly answer your question, but I assume you want to do some byte-mangling (wild guess) as you were not very explicit in your question.
If that's the case, then here's another "thing" you might want to do:
data = [int(_, 16) for _ in string1.split('0x')[1:]]
This will give you a list of ints representing the "byte"(?) values (of course depends what you want to do...)
The above code makes a couple of assumptions based on your example input:
The input string will start with 0x
The input string does not include any non-escaped values. In other words, you always have a sequence of 0xnn strings with nothing else in between!

Breaking 1 String into 2 Strings based on special characters using python

I am working with python and I am new to it. I am looking for a way to take a string and split it into two smaller strings. An example of the string is below
wholeString = '102..109'
And what I am trying to get is:
a = '102'
b = '109'
The information will always be separated by two periods like shown above, but the number of characters before and after can range anywhere from 1 - 10 characters in length. I am writing a loop that counts characters before and after the periods and then makes a slice based on those counts, but I was wondering if there was a more elegant way that someone knew about.
Thanks!
Try this:
a, b = wholeString.split('..')
It'll put each value into the corresponding variables.
Look at the string.split method.
split_up = [s.strip() for s in wholeString.split("..")]
This code will also strip off leading and trailing whitespace so you are just left with the values you are looking for. split_up will be a list of these values.

Categories

Resources