I've got a two letter word that I'd like to attach to a double digit number. The word is an integer and the number is a string.
Say the name of the number is "number" and the name of the word is "word".
How would you make it print both of them together without spaces. When I try it right now it still has a space between them regardless of what I try.
Thanks !
'{}{}'.format(word, number)
For example,
In [19]: word='XY'
In [20]: number=123
In [21]: print('{}{}'.format(word, number))
XY123
The print function has a sep parameter that controls spacing between items:
print(number, word, sep="")
If you need a string, rather than printing, than unutbu's answer with string formatting is better, but this may get you to your desired results with fewer steps.
In python 3 the preferred way to construct strings is by using format
To print out a word and a number joined together you would use:
print("{}{}".format(word, number))
Related
I have a list of strings that look something like this:
"['id', 'thing: 1\nother: 2\n']"
"['notid', 'thing: 1\nother: 2\n']"
I would now like to read the value of 'other' out of each of them.
I did this by counting the number at a certain position but since the position of such varies I wondererd if I could read from a certain character like a comma and say: read x_position character from comma. How would I do that?
Assuming that "other: " is always present in your strings, you can use it as a separator and split by it:
s = 'thing: 1\nother: 2'
_,number = s.split('other: ')
number
#'2'
(Use int(number) to convert the number-like string to an actual number.) If you are not sure if "other: " is present, enclose the above code in try-except statement.
I have this string:
abc,12345,abc,abc,abc,abc,12345,98765443,xyz,zyx,123
What can I use to add a 0 to the beginning of each number in this string? So how can I turn that string into something like:
abc,012345,abc,abc,abc,abc,012345,098765443,xyz,zyx,0123
I've tried playing around with Regex but I'm unsure how I can use that effectively to yield the result I want. I need it to match with a string of numbers rather than a positive integer, but with only numbers in the string, so not something like:
1234abc567 into 01234abc567 as it has letters in it. Each value is always separated by a comma.
Use re.sub,
re.sub(r'(^|,)(\d)', r'\g<1>0\2', s)
or
re.sub(r'(^|,)(?=\d)', r'\g<1>0', s)
or
re.sub(r'\b(\d)', r'0\1', s)
Try following
re.sub(r'(?<=\b)(\d+)(?=\b)', r'\g<1>0', str)
If the numbers are always seperated by commas in your string, you can use basic list methods to achieve the result you want.
Let's say your string is called x
y=x.split(',')
x=''
for i in y:
if i.isdigit():
i='0'+i
x=x+i+','
What this piece of code does is the following:
Splits your string into pieces depending on where you have commas and returns a list of the pieces.
Checks if the pieces are actually numbers, and if they are a 0 is added using string concatenation.
Finally your string is rebuilt by concatenating the pieces along with the commas.
Hey guys I'm a newbie in python programming. As the title suggests char is getting printed instead of integer for the following code snippet. Thanks.
nl=[]
inp=raw_input()
if inp.isdigit():
list=inp.split()
for s in list:
nl.append(int(s))
print nl
For example if I give an input as 1 2 3 the output obtained is ['1','2','3'] but the expected output is [1,2,3]. I want to print the answer in a list form.
Try this:
nl = [int(n) for n in raw_input().split(' ') if n.isdigit()]
print nl
You have to split the input before to use str.isdigit() because '1 2 3' is not a digit.
Warning: if the numbers can be floating point numbers, you should use a regex instead of str.isdigit()
Being a novice myself, I can somewhat understand your situation.
So lets solve all the issues here:
inp will take a string from the user. Ex: "12345", "12 45" etc.
inp.isdigit() checks whether the string inp is composed of digits or not. Mind you, we are strictly speaking about digits (no other special characters).
Ex: "12345".isdigit() returns True while "12 45".isdigit() returns False.
inp.split() will return a list of words (or items) separated by a white space. Since, isdigit() and split() are contradicting each other, you are not getting a correct answer for "12 45" (or anything like this).
Hope you get my point.
Tip : Don't use list are a variable name. The code may work fine but it is not conventional.
I like the list comprehension above but here's the same thing (without the use of isdigit) using a for loop-
nl = []
inp = raw_input()
lst = inp.split(' ')
for s in lst:
nl.append(int(s))
print nl
A couple of quick points. Spaces before and after the equal sign (=) improves readability. You probably should avoid using list as a variable name since list is a built-in function. raw_input returns a string so "if inp.isdigit()" will always return false in your code.
x="I use computers"
print (x)
y=x[0:1]
y1=x[2:5]
y2=x[6:15]
n=(y+y1+y2)
print len(n)
I know this counts the number of letters but how do I count the number of words in the sentence?
If you're just interested in counting the words, not in splitting the string into words, split() does unnecessary work. By counting the number of spaces and adding one, you get the number of words much faster. Though this does assume that all words are separated by a single space, not more.
Proof:
>>>import timeit
>>> timeit.timeit("len(x.split())", setup='x="I use computers"' , number=10**6)
0.28843931717636195
>>> timeit.timeit("x.count(' ')+1", setup='x="I use computers"' , number=10**6)
0.19020372901493232
Try this piece of code
x = "I use computers"
print len(x.split())
I'm new to regex, and I'm starting to sort of get the hang of things. I have a string that looks like this:
This is a generated number #123 which is an integer.
The text that I've shown here around the 123 will always stay exactly the same, but it may have further text on either side. But the number may be 123, 597392, really one or more digits. I believe I can match the number and the folowing text using using \d+(?= which is an integer.), but how do I write the look-behind part?
When I try (?<=This is a generated number #)\d+(?= which is an integer.), it does not match using regexpal.com as a tester.
Also, how would I use python to get this into a variable (stored as an int)?
NOTE: I only want to find the numbers that are sandwiched in between the text I've shown. The string might be much longer with many more numbers.
You don't really need a fancy regex. Just use a group on what you want.
re.search(r'#(\d+)', 'This is a generated number #123 which is an integer.').group(1)
if you want to match a number in the middle of some known text, follow the same rule:
r'some text you know (\d+) other text you also know'
res = re.search('#(\d+)', 'This is a generated number #123 which is an integer.')
if res is not None:
integer = int(res.group(1))
You can just use the findall() in the re module.
string="This is a string that contains #134534 and other things"
match=re.findall(r'#\d+ .+',string);
print match
Output would be '#1234534 and other things'
This will match any length number #123 or #123235345 then a space then the rest of the line till it hits a newline char.
if you want to get the numbers only if the numbers are following text "This is a generated number #" AND followed by " which is an integer.", you don't have to do look-behind and lookahead. You can simply match the whole string, like:
"This is a generated number #(\d+) which is an integer."
I am not sure if I understood what you really want though. :)
updated
In [16]: a='This is a generated number #123 which is an integer.'
In [17]: b='This should be a generated number #123 which could be an integer.'
In [18]: exp="This is a generated number #(\d+) which is an integer."
In [19]: result =re.search(exp, a)
In [20]: int(result.group(1))
Out[20]: 123
In [21]: result = re.search(exp,b)
In [22]: result == None
Out[22]: True