Split a string if character is present else don't split - python

I have a string like below in python
testing_abc
I want to split string based on _ and extract the 2 element
I have done like below
split_string = string.split('_')[1]
I am getting the correct output as expected
abc
Now I want this to work for below strings
1) xyz
When I use
split_string = string.split('_')[1]
I get below error
list index out of range
expected output I want is xyz
2) testing_abc_bbc
When I use
split_string = string.split('_')[1]
I get abc as output
expected output I want is abc_bbc
Basically What I want is
1) If string contains `_` then print everything after the first `_` as variable
2) If string doesn't contain `_` then print the string as variable
How can I achieve what I want

Set the maxsplit argument of split to 1 and then take the last element of the resulting list.
>>> "testing_abc".split("_", 1)[-1]
'abc'
>>> "xyz".split("_", 1)[-1]
'xyz'
>>> "testing_abc_bbc".split("_", 1)[-1]
'abc_bbc'

You can use list slicing and str.join in case _ is in the string, and you can just get the first element of the split (which is the only element) in the other case:
sp = string.split('_')
result = '_'.join(sp[1:]) if len(sp) > 1 else sp[0]

All of the ways are good but there is a very simple and optimum way for this.
Try:
s = 'aaabbnkkjbg_gghjkk_ttty'
try:
ans = s[s.index('_')+1:]
except:
ans = s

Ok so your error is supposed to happen/expected because you are using '_' as your delimiter and it doesn't contain it.
See How to check a string for specific characters? for character checking.
If you want to only split iff the string contains a '_' and only on the first one,
input_string = "blah_end"
delimiter = '_'
if delimiter in input_string:
result = input_string.split("_", 1)[1] # The ",1" says only split once
else:
# Do whatever here. If you want a space, " " to be a delimiter too you can try that.
result = input_string

this code will solve your problem
txt = "apple_banana_cherry_orange"
# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split("_", 1)
print(x[-1])

Related

Replacing The Tab Character In A String

Let's say I have a string like this:
a = a = "\t\t\t\t"
If I print out the count of "\t" in the string, this is the output:
print(a.count("\t")) == output = 4 \
If I wanted to replace "\t" at any given occurrence in that string, how would I do it?
ex:
a.replace("\t", "a") #replacing the (first occurrence?) of "\t?
print(a.count("\t")) == output = 3
However, the "\t" is not getting replaced by "a" and therefore the "\t" count is still 4. Is there a way I can do this; preferably replace any given occurrence of "\t" within the string?
You can use regular expressions. It is another way to replace all the occurencies by specific pattern
try to use expandtabs, such as text = text.expandtabs(tabsize=4)
As Michael Butscher says, str.replace() returns a new string with replacements done. By default, all occurrences are replaced. Tabs are not a special case. This should do it:
a = a.replace("\t", "a")
print(a.count("\t"))
See: https://docs.python.org/3/library/stdtypes.html?highlight=str%20replace#str.replace
Thank you for the responses as they really opened the door to the solution. What I have done is converted a into a list:
a = "\t\t\t\t"
b = a.list()
#print(b):
#['\t', '\t', '\t', '\t']
I can then just use indices to replace what I need b[0] = "a", and then just convert it back into a str when I am done. This suits my purpose, but there are multiple ways of dealing with it like if I wanted to change all the "\t" into a "a" I could do a simple iteration like this:
for x in range(0, len(b)):
if b[x] == "\t":
b[x] = "a"
Of course the replace() function could do the same with it in string format, but at least with this iteration I could add more conditional logic if needed.

Remove first comma in a string with Python

I have this string
s = "1,395,54"
I would like to remove the first comma to obtain the following string:
s = "1395,54"
What is the most efficient way to solve this simple problem?
You can use str.replace, it takes a third argument which specifies the number of occurrences to replace.
>>> your_str = "1,395,54"
>>> your_str.replace(",", "", 1)
'1395,54'
By slicing the string. The find method return the index of the first match
s = "1,395,54"
index = s.find(',')
print(s[:index] + s[index+1:])

How to edit a string to add a variable at a particular place in python?

I have a variable:
a = 2
and I have this string:
"//media.boohooman.com/i/boohooman/mzz11035_black_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
I want edit this string so I can add variable, a, at a specific place in this string, after "mzz11035_black_xl", to make the whole string look like:
"//media.boohooman.com/i/boohooman/mzz11035_black_xl_2/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
What options do I have to achieve this goal. I know that there are some hard coded ways where I can count the characters before and after a specific place and do slicing, but I am looking for some more general method so that it would work even if the strings change a bit. eg.
"//media.boohooman.com/i/boohooman/mzz11035_blue_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
or
"//media.boohooman.com/i/boohooman/mzz11035_blue_s/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
Thanks.
You need to use f string or the format() function:
var = "_2"
f"//media.boohooman.com/i/boohooman/mzz11035_black_xl{var}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
or
var = "_2"
"//media.boohooman.com/i/boohooman/mzz11035_black_xl{}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp".format(var)
if the position of the "mzz11035_black_xl" is changing you can do this:
var = "_2"
split_with = "mzz11035_black_xl"
initial_string = "//media.boohooman.com/i/boohooman/mzz11035_black_xl{var}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
# split the string into two parts
split_string = initial_string.split(split_with)
# Add the parts back with any sting in between
resulting_string = split_string[0] + split_with + var + split_string[1]
but in this case, you need to make sure that you have only one "mzz11035_black_xl" in your string.
If the string is changing but the link structure doesn't change you can try splitting with "/" (not elegant now but can be optimized)
var = "_2/"
split_with = "/"
initial_string = "//media.boohooman.com/i/boohooman/mzz11035_blue_s/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
# initializing K
K = 6
# printing original string
print("The original string is : " + str(initial_string))
# Using split() + join()
# Split string on Kth Occurrence of Character
temp = initial_string.split(split_with)
resulting_tuple = split_with.join(temp[:K]), split_with.join(temp[K:])
# Convert to list and insert any string
resulting_list = list(resulting_tuple)
resulting_list.insert(1,var)
# convert to string
resulting_string = ""
resulting_string = resulting_string.join(list(resulting_list))
print("Is list after Kth split is: " + resulting_string)
Output:
"Is list after Kth split is : //media.boohooman.com/i/boohooman/mzz11035_blue_s_2/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
you can use replace() function.
Syntax:
string.replace(old, new, count)
Parameters:
old – old substring you want to replace.
new – new substring which would replace the old substring.
count – the number of times you want to replace the old substring with the new substring. (Optional)
Return Value:
It returns a copy of the string where all occurrences of a substring are replaced with another substring.
Code sample:
a = "2"
str_to_replace = "//media.boohooman.com/i/boohooman/mzz11035_black_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
old = "mzz11035_black_xl"
new = "mzz11035_black_xl_{}".format(a)
str_replaced = str_to_replace.replace(old, new)
print(str_replaced)

How to partial split and take the first portion of string in Python?

Have a scenario where I wanted to split a string partially and pick up the 1st portion of the string.
Say String could be like aloha_maui_d0_b0 or new_york_d9_b10. Note: After d its numerical and it could be any size.
I wanted to partially strip any string before _d* i.e. wanted only _d0_b0 or _d9_b10.
Tried below code, but obviously it removes the split term as well.
print(("aloha_maui_d0_b0").split("_d"))
#Output is : ['aloha_maui', '0_b0']
#But Wanted : _d0_b0
Is there any other way to get the partial portion? Do I need to try out in regexp?
How about just
stArr = "aloha_maui_d0_b0".split("_d")
st2 = '_d' + stArr[1]
This should do the trick if the string always has a '_d' in it
You can use index() to split in 2 parts:
s = 'aloha_maui_d0_b0'
idx = s.index('_d')
l = [s[:idx], s[idx:]]
# l = ['aloha_maui', '_d0_b0']
Edit: You can also use this if you have multiple _d in your string:
s = 'aloha_maui_d0_b0_d1_b1_d2_b2'
idxs = [n for n in range(len(s)) if n == 0 or s.find('_d', n) == n]
parts = [s[i:j] for i,j in zip(idxs, idxs[1:]+[None])]
# parts = ['aloha_maui', '_d0_b0', '_d1_b1', '_d2_b2']
I have two suggestions.
partition()
Use the method partition() to get a tuple containing the delimiter as one of the elements and use the + operator to get the String you want:
teste1 = 'aloha_maui_d0_b0'
partitiontest = teste1.partition('_d')
print(partitiontest)
print(partitiontest[1] + partitiontest[2])
Output:
('aloha_maui', '_d', '0_b0')
_d0_b0
The partition() methods returns a tuple with the first element being what is before the delimiter, the second being the delimiter itself and the third being what is after the delimiter.
The method does that to the first case of the delimiter it finds on the String, so you can't use it to split in more than 3 without extra work on the code. For that my second suggestion would be better.
replace()
Use the method replace() to insert an extra character (or characters) right before your delimiter (_d) and use these as the delimiter on the split() method.
teste2 = 'new_york_d9_b10'
replacetest = teste2.replace('_d', '|_d')
print(replacetest)
splitlist = replacetest.split('|')
print(splitlist)
Output:
new_york|_d9_b10
['new_york', '_d9_b10']
Since it replaces all cases of _d on the String for |_d there is no problem on using it to split in more than 2.
Problem?
A situation to which you may need to be careful would be for unwanted splits because of _d being present in more places than anticipated.
Following the apparent logic of your examples with city names and numericals, you might have something like this:
teste3 = 'rio_de_janeiro_d3_b32'
replacetest = teste3.replace('_d', '|_d')
print(replacetest)
splitlist = replacetest.split('|')
print(splitlist)
Output:
rio|_de_janeiro|_d3_b32
['rio', '_de_janeiro', '_d3_b32']
Assuming you always have the numerical on the end of the String and _d won't happen inside the numerical, rpartition() could be a solution:
rpartitiontest = teste3.rpartition('_d')
print(rpartitiontest)
print(rpartitiontest[1] + rpartitiontest[2])
Output:
('rio_de_janeiro', '_d', '3_b32')
_d3_b32
Since rpartition() starts the search on the String's end and only takes the first match to separate the terms into a tuple, you won't have to worry about the first term (city's name?) causing unexpected splits.
Use regex's split and keep delimiters capability:
import re
patre = re.compile(r"(_d\d)")
#👆 👆
#note the surrounding parenthesises - they're what drives "keep"
for line in """aloha_maui_d0_b0 new_york_d9_b10""".split():
parts = patre.split(line)
print("\n", line)
print(parts)
p1, p2 = parts[0], "".join(parts[1:])
print(p1, p2)
output:
aloha_maui_d0_b0
['aloha_maui', '_d0', '_b0']
aloha_maui _d0_b0
new_york_d9_b10
['new_york', '_d9', '_b10']
new_york _d9_b10
credit due: https://stackoverflow.com/a/15668433

Python: Remove First Character of each Word in String

I am trying to figure out how to remove the first character of a words in a string.
My program reads in a string.
Suppose the input is :
this is demo
My intention is to remove the first character of each word of the string, that is
tid, leaving his s emo.
I have tried
Using a for loop and traversing the string
Checking for space in the string using isspace() function.
Storing the index of the letter which is encountered after the
space, i = char + 1, where char is the index of space.
Then, trying to remove the empty space using str_replaced = str[i:].
But it removed the entire string except the last one.
List comprehensions is your friend. This is the most basic version, in just one line
str = "this is demo";
print " ".join([x[1:] for x in str.split(" ")]);
output:
his s emo
In case the input string can have not only spaces, but also newlines or tabs, I'd use regex.
In [1]: inp = '''Suppose we have a
...: multiline input...'''
In [2]: import re
In [3]: print re.sub(r'(?<=\b)\w', '', inp)
uppose e ave
ultiline nput...
You can simply using python comprehension
str = 'this is demo'
mstr = ' '.join([s[1:] for s in str.split(' ')])
then mstr variable will contains these values 'his s emo'
This method is a bit long, but easy to understand. The flag variable stores if the character is a space. If it is, the next letter must be removed
s = "alpha beta charlie"
t = ""
flag = 0
for x in range(1,len(s)):
if(flag==0):
t+=s[x]
else:
flag = 0
if(s[x]==" "):
flag = 1
print(t)
output
lpha eta harlie

Categories

Resources