How can i remove (') from a string result, in order to use in subproduct i need not to have (') - python-3.x

Im creating a name based on different inputs by the user, it constructs this way:
Result = str('Blue' + shoetype.result + '5')
Result = 'Bluesandal5'
now i need to use 'Bluesandal5' in another operation, but i need it with out the ('), just Bluesandal5
how can I achieve this?

The ' characters are there for the specific purpose of telling you that it's a string. They disappear when you actually use the string for something. Example, using my python console:
>>> Result = "Bluesandal5"
>>> Result
'Bluesandal5'
>>> print(Result)
Bluesandal5
As you can see, the quotes disappear when it's used in a print() statement. This also holds true for any other operation (e.g. string slicing) - they don't count as part of the string itself.

Related

If input() returns a string, why doesn't print() display the quotation marks?

I'm having trouble understanding the following:
According to my book, unless otherwise specified, input will return a string type. If a string is printed wouldn't you expect the quotes to be included in the result? Is this just how print() is designed to work, if so why?
Example problem:
x = input() # user enters 5.5
print(x) # i expect '5.5' to be printed, instead 5.5 is printed
Wouldn't it be better to print the variable x for exactly what it is?
No, you use quotes to create a literal string; quotes are not part of the string value itself. If you want to see the quotes, ask Python for the representation of your string, i.e.
print(repr(x))

re.sub replacing string using original sub-string

I have a text file. I would like to remove all decimal points and their trailing numbers, unless text is preceding.
e.g 12.29,14.6,8967.334 should be replaced with 12,14,8967
e.g happypants2.3#email.com should not be modified.
My code is:
import re
txt1 = "9.9,8.8,22.2,88.7,morris1.43#email.com,chat22.3#email.com,123.6,6.54"
txt1 = re.sub(r',\d+[.]\d+', r'\d+',txt1)
print(txt1)
unless there is an easier way of completing this, how do I modify r'\d+' so it just returns the number without a decimal place?
You need to make use of groups in your regex. You put the digits before the '.' into parentheses, and then you can use '\1' to refer to them later:
txt1 = re.sub(r',(\d+)[.]\d+', r',\1',txt1)
Note that in your attempted replacement code you forgot to replace the comma, so your numbers would have been glommed together. This still isn't perfect though; the first number, since it doesn't begin with a comma, isn't processed.
Instead of checking for a comma, the better way is to check word boundaries, which can be done using \b. So the solution is:
import re
txt1 = "9.9,8.8,22.2,88.7,morris1.43#email.com,chat22.3#email.com,123.6,6.54"
txt1 = re.sub(r'\b(\d+)[.]\d+\b', r'\1',txt1)
print(txt1)
Considering these are the only two types of string that is present in your file, you can explicitly check for these conditions.
This may not be an efficient way, but what I have done is split the str and check if the string contains #email.com. If thats true, I am just appending to a new list. For your 1st condition to satisfy, we can convert the str to int which will eliminate the decimal points.
If you want everything back to a str variable, you can use .join().
Code:
txt1 = "9.9,8.8,22.2,88.7,morris1.43#email.com,chat22.3#email.com,123.6,6.54"
txt_list = []
for i in (txt1.split(',')):
if '#email.com' in i:
txt_list.append(i)
else:
txt_list.append(str(int(float(i))))
txt_new = ",".join(txt_list)
txt_new
Output:
'9,8,22,88,morris1.43#email.com,chat22.3#email.com,123,6'

Remove part of string (regular expressions)

I am a beginner in programming. I have a string for example "test:1" and "test:2". And I want to remove ":1" and ":2" (including :). How can I do it using regular expression?
Hi andrew it's pretty easy. Think of a string as if it is an array of chars (letters) cause it actually IS. If the part of the string you want to delete is allways at the end of the string and allways the same length it goes like this:
var exampleString = 'test:1';
exampleString.length -= 2;
Thats it you just deleted the last two values(letters) of the string(charArray)
If you cant be shure it's allways at the end or the amount of chars to delete you'd to use the version of szymon
There are at least a few ways to do it with Groovy. If you want to stick to regular expression, you can apply expression ^([^:]+) (which means all characters from the beginning of the string until reaching :) to a StringGroovyMethods.find(regexp) method, e.g.
def str = "test:1".find(/^([^:]+)/)
assert str == 'test'
Alternatively you can use good old String.split(String delimiter) method:
def str = "test:1".split(':')[0]
assert str == 'test'

Python 3 Nesting String format

I'm trying to generate a simple triangle shape with numbers, where the output might look like this:
1
22
333
However, I can't get this to work with a nested String format, the best I can achieve is the following...
for i in range(1, size+1):
line = f'{i}' *i
print(f"{line:>{size}}")
Ideally, I'd like to have 'line' nested within the print method. Any suggestions?
Your "nested" format string needs to be formatted twice. Any curly braces you want to keep for the second time needs to be escaped in the first, thus { becomes {{ and } becomes }}. Also, since you can't use the f prefix twice, You can explicitly call format for the second formatting.
What you're looking for is this:
for i in range(1, size + 1):
line = f"{i}" * i
print(f"{{0:>{size}}}".format(line))
So first string formatting turns f"{{0:>{size}}}" into {0:>3}, thus when reaching the explicit calling of format you basically get print("{0:>3}".format(line)).
Actually if what you want is to embed the "line" creation within the f-string itself, you can achieve it with:
for i in range(1, size+1):
print(f"{str(i) * i:>{size}}")
Alternatively you can also form a string with the old % operator, in there, if needed.
for i in range(1, size+1):
print(f"{'%s' % (i) * i:>{size}}")

Python - exclude data with double quotes and parenthesis

I have a list which contains set of words in single quotes and double quotes, now i want to grep all the data only in single quotes.
Input_
sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
Output:
telco_name_list.event_date,reference.date,data_flow.code
I want to exclude the parenthesis and string in double quotes.
Since you tagged the post as python-3.x, I'm assuming you want to write python code. Based on your example this would work:
#!/usr/bin/env python3
Input_sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
comma = False
result = []
for i in range(len(Input_sentence)):
e = Input_sentence[i]
if e.startswith("'"): continue
e = e.replace('(', '').replace(')', '')
if comma:
print(",", end="")
comma = True
print(e, end="")
print()
This code iterates through the list, ignoring elements which begin with a single quote and filtering out parentheses from elements before printing them on stdout. This code works for the given example, but may or may not be what you need as the exact semantics of what you want out are somewhat ambiguous (i.e. is it fine to filter out all parentheses or just the ones at the beginning and end of your elements?).

Resources