Removing strings character with given input - python

I need to remove a certain string with the given input. For example,
user_input = input("Input File name: ")
and the user key in "patient_zero[20150203].txt" and it would like to clean up the file name by removing all instances of square brackets and the contents contained within it.
Output should be "patient_zero.txt". Is there anyway to do it?

If you just want to remove the square bracket portion of the filename, you could use:
inp = "patient_zero[20150203].txt"
output = re.sub(r'^(\S+)\[.*?\]\.(\S+)$', r'\1.\2', inp)
print(output) # patient_zero.txt

using spplit
var = "patient_zero[20150203].txt"
print(var.split("[")[0] + var.split("]")[1]) # PatientName + FileFormat

import re
s = "patient_zero[20150203].txt"
print (re.sub(r'\[[\w\s]+\]','',s))
Output:
patient_zero.txt

Related

how to remove space in string list python

So i have list contains with 1 string.
biner_pass = ['00001111000000000000111111111111
00001111111111111111000011111111 11111111111100000000000000000000
11111111111111110000111111111111']
all i want is to remove the space and join all the binary.
i was trying using
binerpass = ''.join(biner_pass.split(' ')) or biner_pass.replace(" ", "") but it cannot work. so how to remove space?
The string is the 0-th element of a list with 1 element. Therefore you need
biner_pass[0].replace(" ", "")
IIUC, You can use replace.
biner_pass = ['0000111100000000000011111111111100001111111111111111000011111111 1111111111110000000000000000000011111111111111110000111111111111']
biner_pass[0] = biner_pass[0].replace(" ", "")
print(biner_pass)
Output:
['00001111000000000000111111111111000011111111111111110000111111111111111111110000000000000000000011111111111111110000111111111111']
You need to do it for each element of the list
biner_pass = [b.replace(' ','') for b in biner_pass]
binary will be a string that conatains 1 and 0 without spaces
binary = "".join(biner_pass).replace(" ", "")

New line character as user input in Python

While joining items in a list and printing them out as a string, I wanted to take user input for a character that will be used a separator. What if the separator is to be a newline? What can the user input be? Because if the user input is simply '\n' then that is being used literally ('\n' is being added as text).
Code that I ran:
tojoin = ['Hello','Its','Me','Uh','Mario']
merge = tojoin[0]
sepchar = input("Enter the character(s) with which you wish to separate the list items: ")
#User input = '\n'
for i in tojoin:
merge = merge+sepchar+i
print(merge)
Expected output:
Hello
It's
Me
Uh
Mario
Actual output:
Hello\nHello\nIts\nMe\nUh\nMario
How can I get to the expected output?
you need to add the following line of code after the input
sepchar = bytes(sepchar, "utf-8").decode("unicode_escape")
source

Does string formatting not work within an input() function?

My code:
new_account = sys.argv[1]
confirm_new = input("Would you like to add {} to the dictionary?" +
"\ny or n\n".format(new_account))
This doesn't format the string to place the variable in place of {}. What's up?
This has nothing to do with input. It's just that addition has lower precedence than method calls:
>>> "{}" + "b".format('a')
'{}b'
Normally I just use automatic string concatenation if I have a multi-line string (just omit the +):
confirm_new = input("Would you like to add {} to the dictionary?"
"\ny or n\n".format(new_account))

need help converting string to unicode and then add values

I need help converting this code to lower cases, then substring to remove spaces then find the ASCII values then add them up to get a sum, this is what I wrote:
def main ():
# Handshake
print("This program computes a total value for a user's full name")
# Prompt and read input
fullname = input("Please enter your full name: ")
# Convert user's full name to all lower case letters
fullname = fullname.lower()
# Split user's full name into substrings
fullname = fullname.split()
# Loop through each substring and compute
# total ASCII value of user's name
totalvalue = 0
for character in fullname:
fullname = ord(character) + totalvalue
#Display outputs
print("\nYour name is: ", fullname)
print("\nThe value of your name is: ", totalvalue)
main()
Two problems: (1) After splitting, fullname is now a list of names, so you need a second loop to iterate over the characters, and (2) you typed fullname instead of totalvalue when summing in the loop. Try replacing the loop with:
totalvalue = 0
for name in fullname:
for character in name:
totalvalue += ord(character)
What about something like this where you use a list comprehension:
import sys
def main ():
# Prompt and read input
text= raw_input().decode(sys.stdin.encoding)
values = [ord(c) for c in text]
print(values)
main()
where values is a list of the ascii values for the string, you can prob do the rest from there

Python - Print last string after line.split

Is there a easy way to print the last string after you've split?
Input File:
Name:DOB:Age
Name:Name2:DOB:Age
I want the name and age...
So...
string = line.split(':')
print(string[0] + string[xxxx])
Is there anything that goes where xxxx is to always print the last string?
Thanks
string = line.split(':')
print(string[0] + string[-1])

Categories