Python add dashes to your characters in a string? [duplicate] - python

This question already has answers here:
Returning original string with symbols between each character
(2 answers)
Closed 9 months ago.
I have a string that i want to modify.
The string is something like 0rty4653 and i want to convert it into this format :-
0--r--t--y--4--6--5--3-- or in other words make it look into the following format --[CHAR]--.
The string could be dynamic.
I converted my string into a list thus far :-
my_str = 0rty4653
my_list = my_str.split()
but how do i convert it into the above format?

final = ""
for i in my_list:
final += i+"--"
iterating over the list should allow you to concat each element and -- to create that product

Related

How to remove some words after a specific character in a list in python [duplicate]

This question already has answers here:
How do I get the filename without the extension from a path in Python?
(31 answers)
Closed 2 years ago.
So my question is:
I will have an array. which contains ["something.json", "something2.json", "something3.json"].
I want to remove every .json in this array and get an output which is similar to like ["something", "something2", "something3"] using Python. Thanks for helping me out.
You can use .endswith() to find the elements that have that file type and then slice everything except the suffix.
my_list = ["something.json", "something2.json"]
suffix = ".json"
my_list = [x[:-len(suffix)] for x in my_list if x.endswith(suffix)]
If you are using python3.9 you could use new string method to remove the suffixes
str.removesuffix(suffix, /):
If the string ends with the suffix string
and that suffix is not empty, return string[:-len(suffix)]. Otherwise,
return a copy of the original string:
>>> 'MiscTests'.removesuffix('Tests')
'Misc'

Extracting a substring from string that is the last instance after a space in python [duplicate]

This question already has answers here:
How to grab number after word in python
(4 answers)
Closed 2 years ago.
I am new to python manipulation of strings.
I have a collection of strings that have a similar format, two examples of the strings are as follows, ex1: "Graduated in 2015", ex2: "Graduates in 2022". The portion of these strings that I would like to extract are the years that is the string that is the last string without spaces. Using the above examples I would like to have an output of something like '2015' and '2020' and save these to a list. Is there a way to extract a substring using this criteria and save these to a list?
visually this is what I want
str1<-"Graduated in 2015"
str2<-"Graduates in 2022"
#some code extracting the year substring and adding them to a list l
print(l)
['2015','2022']
list_of_strings = ["Graduated in 2015", "Graduates in 2022"]
l = [s.split()[-1] for s in list_of_strings]
print(l) # ['2015', '2022']
If it is in a fixed pattern you can split and use negative indexing:
str1 = "Graduated in 2015"
str2 = "Graduates in 2022"
print([int(str1.split()[-1]),int(str2.split()[-1])])

Get each element in a list string [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
I have a string that consists of a list which made of different product code.
"['B00GHX7H0A', 'B00FRERO7G', 'B00R68QXCS', 'B000Z65AZE', 'B07GFHJRMX', 'B074KGBGL7', 'B00R68QXJG', 'B00025WYZC', 'B07H3W9BM5', 'B00KOBT82G', 'B072N2M1P6', 'B071G8FG2N', 'B00FASVFI8', 'B00GHXE4N8', 'B00EPG2QJI', 'B01MQ4MEFE', 'B01M8ML0SY', 'B074KHCPLH', 'B004XQWY4W', 'B00FASV6UU', 'B01M31HJBJ', 'B00KC8TU7O', 'B00B9TU5T2', 'B00K75EZ04', 'B000Q2Y0FI', 'B00FEGOCCM', 'B00EPFXFBW', 'B00H6SQY3Q', 'B00HZAOWUC', 'B07GFJF1DN', 'B001WBS68E', 'B074KJZCPH']"
How can I extract each product code out of this string?
There is nother option than to replace the unnecessary characters and then splitting it by the commas:
myList = "['123','231','312','123']"
myList = myList.replace("[","").replace("]","").replace("'", "").split(",")
print(myList)
# output: ["123","231","312","123"]
But with this you would get problems if the items inside the list could potentially contain commas.

How to get the numbers from a string (contains no spaces between letters and numbers)? [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 3 years ago.
So, I have a string "AB256+74POL". I want to extract the numbers only into a list say num = [256,74]. How to do this in python?
I have tried string.split('+') and followed by iterating over the two parts and adding the characters which satisfy isdigit(). But is there an easier way to that?
import re
a = 'AB256+74POL'
array = re.findall(r'[0-9]+', a)
"".join([c if c.isdigit() else " " for c in mystring]).split()
Explanation
Strings are iterable in python. So we iterate on each character in the string, and replace non digits with spaces, then split the result to get all sequences of digits in a list.

List element case conversion [duplicate]

This question already has answers here:
Convert a list with strings all to lowercase or uppercase
(13 answers)
Closed 4 years ago.
I have a list that has 12 elements. I am getting an input and matching that input with the value of another variable. Now that means that case-sensitivity will be a problem. I know how to go through the list with a loop but how can I convert every character in each element to a lowercase character?
for i in sa:
# something here to convert element in sa to lowercase
A simple one liner:
lowercase_list = [ i.lower() for i in input_list ]

Categories

Resources