I am using python3 right now. I got a string over here.
How do i print the -57 which is the 5th number counting from right('-' means no valid value)and print it.
I have tried the many ways but don't work for me.
Thank you guys.
525,03,4A5E31F,32,1850,3,5,5,A85E,-87,-8,-57,13,255,80,-
print(text[-5]) will this work?
Split the sting after each ',' and print the 5th to last element.
my_str = '525,03,4A5E31F,32,1850,3,5,5,A85E,-87,-8,-57,13,255,80,-'
print(my_str.split(',')[-5])
Related
I have run into an issue where i can't format a string to be printed.
The function is suppossed to convert Binary into Text which is does brilliantly but the printed out result is formatted all the way the right and not the left.
I have tried resolving this by looking up how to format the string but im getting no luck. Im hoping someone can resolve this issue for me.
Heres the code:
elif Converter_Choice2 == str(3):
def Bin_to_Txt():
print("\nYour Message in Binary:")
bin_input = input("")
binary_int = int(bin_input, 2)
byte_number = binary_int.bit_length() + 7 // 8
binary_array = binary_int.to_bytes(byte_number, "big")
ascii_text = binary_array.decode()
clear()
print("\nYour Message in Text:")
print(ascii_text)
Bin_to_Txt()
I tried different ways to format it but im still new to Python 3. I tried putting "ascii_text" into another string to format it, so i could print that string but it didn't work.
ascii_text_formatted = ("{:<15}".format(ascii_text))
print(ascii_text_formatted)
Some advice for this would be great.
Heres a quick Binary code that can be used: 0100100001100101011011000110110001101111
The decoded version should say "Hello".
I managed to find the answer. If anyone else has this issue or something similar try this:
The issue was the variable "binary_array" was printing out invisible numbers before the printed answer in this case "Hello". Due to this it would print "Hello" all the way to the right as the invisible numbers where in front of it.
To fix this issue i added [34:] at the end of the "binary_array" string to remove those invisible numbers from the print. By adding [34:] it means the first 34 characters/numbers wont be printed even if they are invisible. So this can be any number that you need it to be. For example if i changed 34 to 35 it would remove the "H" from "Hello" and print "ello".
Heres some screenshots of the function block and printed responces from before and after adding [34:].
https://imgur.com/a/W25G1FZ
I am having a string as follows:
A5697[2:10] = {ravi, rageev, raghav, smith};
I want the content after "A5697[2:10] =". So, my output should be:
{ravi, rageev, raghav, smith};
This is my code:
print(re.search(r'(?<=A\d+\[.*\] =\s).*', line).group())
But, this is giving error:
sre_constants.error: look-behind requires fixed-width pattern
Can anyone help to solve this issue? I would prefer to use regex.
You can try re.sub , like below, Since you have given only one data point. I am assuming all the other data points are following the similar pattern.
import re
text = "A5697[2:10] = {ravi, rageev, raghav, smith}"
re.sub(r'(A\d+\[\d+:\d+\]\s+=\s+)(.+)', r'\2', text)
returns,
'{ravi, rageev, raghav, smith}'
re.sub : substitutes the entire match as given as regex with the 2nd capturing group. The second capturing group captures every thing after '= '.
Simply replace the bits you don't want:
print re.sub(r'A\d[^=]*= *','',line)
See demo here: https://rextester.com/NSG17655
Hello guys for example;
def right_justify( in_str):
print("+" * 5,in_str.rjust(11),"+"*5)
right_justify("Hello")
my function prints out;
and why prints out like this?
+++++ Hello +++++
and ı wannt to print this;
+++++Hello+++++
How can ı make this to make it look more beautiful? :)
This is literally what your code outputs:
('+++++', ' Hello', '+++++')
And that's because of the call to rjust(11) which does exactly what the name implies - it justifies the string 11 characters to the right. Remove that method and your output will be exactly what you want it to be.
Use fstring or str.format
Ex:
def right_justify( in_str):
val = "+"*5
print("{}{}{}".format(val, in_str, val))
print(f"{val}{in_str}{val}")
right_justify("Hello")
Output:
+++++Hello+++++
+++++Hello+++++
I have seen several related posts and several forums to find an answer for my question, but nothing has come up to what I need.
I am trying to use variable instead of hard-coded values in regex which search for either word in a line.
However i am able to get desired result if i don't use variable.
<http://www.somesite.com/software/sub/a1#Msoffice>
<http://www.somesite.com/software/sub1/a1#vlc>
<http://www.somesite.com/software/sub2/a2#dell>
<http://www.somesite.com/software/sub3/a3#Notepad>
re.search(r"\#Msoffice|#vlc|#Notepad", line)
This regex will return the line which has #Msoffice OR #vlc OR #Notepad.
I tried defining a single variable using re.escape and that worked absolutely fine. However i have tried many combination using | and , (pipe and comma) but no success.
Is there any way i can specify #Msoffice , #vlc and #Notepad in different variables and so later i can change those ?
Thanks in advance!!
If I did understand you the right way you'd like to insert variables in your regex.
You are actually using a raw string using r' ' to make the regex more readable, but if you're using f' ' it allows you to insert any variables using {your_var} then construct your regex as you like:
var1 = '#Msoffice'
var2 = '#vlc'
var3 = '#Notepad'
re.search(f'{var1}|{var2}|{var3}', line)
The most annoying issue is that you will have to add \ to escaped char, to look for \ it will be \\
Hope it helped
import re
lines = ["<http://www.somesite.com/software/sub/a1#Msoffice>",
"<http://www.somesite.com/software/sub1/a1#vlc>",
"<http://www.somesite.com/software/sub2/a2#dell>",
"<http://www.somesite.com/software/sub3/a3#Notepad>"]
for line in lines:
if re.search(r'\b(?:\#{}|\#{}|\#{})\b'.format('Msoffice', 'vlc', 'Notepad'), line):
print(line)
Output :
<http://www.somesite.com/software/sub/a1#Msoffice>
<http://www.somesite.com/software/sub1/a1#vlc>
<http://www.somesite.com/software/sub3/a3#Notepad>
This is my string:
"Somestring8/9/0"
I need to get something like this:
['Somestring','8/9/0']
The moment I find a numeric char, I need to split the string to get:
'8/9/0'
This my code:
stringSample = "GigabitEthernet8/9/0"
print re.findall(r'(\w+?)(\d+)', stringSample)[0]
('GigabitEthernet', '8')
But I'm getting this result
What am I doing wrong?
I appreciate your help!!
Your second regex group accepts only digits. Allow it to include forward slashes too.
stringSample = "GigabitEthernet8/9/0"
print re.findall(r'(\w+?)([\d/]+)', stringSample)[0]
# ('GigabitEthernet', '8/9/0')
Try Using the re.split method to split your string in two, passing the maxsplit parameter
re.split('(\w+?)([\d/]+)', stringSample, 1)