Transforming a string to a tuple [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Given a string "key: content" I want to return a tuple (key, content)
I have found some cryptic ways to do it. Is there an easy way to do this in python

As #ShadowRanger suggested, using the tuple() function is one of the easiest ways to convert into a tulpe. We separate the string into two parts by using string.split() function.
So, we implement it this way:
string = "key: content" # given string
mytuple = tuple(string.split(": ")) # split the string from ": " and convert it into a tuple
print(mytuple)
>>> ("key", "content")
Note: This can also be achieved by using for loop, but it is tedious and more time-consuming.

Related

convert a string to a dict when literal_eval not working [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I am trying to convert a pandas column value from str to dict.
The value looks like this :
'""en-US":"!!PLAY""'
I tried with eval, with literal_eval, with json.loads and I only have errors SyntaxError or json.decoder.JSONDecodeError.
The only solution left I see is to find each string with regex and form a dict after. That is pretty heavy to do. Anyone with an idea ?
Split the string at ":" then add them to dictionary use slicing to remove extra quotes
key , val = '""en-US":"!!PLAY""'.split(":")
dic = {key[2:-1]:val[1:-2]}
print(dic)

How to selectively replace characters in a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How would I replace characters in a string for certain indices in Python?
For example, I have version = "00.00.00" and need to change each of the 0s to a different value, say 3, to look like "33.33.33". Also, would this be possible if I had a variable storing this value. If I have vnumber = "3", would I be able to get the same output by using the variable? I'm sure replace() is a good function to use for this, but I'm not sure about syntax.
From an interactive session, you could type:
>>> help(str.replace)
But to answer the question most directly:
vnumber = '3'
newversion = version.replace('0', vnumber)
Is probably what you want to do.
Your guess about str.replace was right. It takes to arguments, the first is the string to be found in the original string, and the second is the string to replace the found occurrences of the first argument with. Code could be like this:
vnumber = "3"
version = "00.00.00"
newversion = version.replace("0", vnumber)
print(newversion)

Using the strip function to strip integer at the ends of a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to strip 0 from a given string.
The string contains either 1 or 0. I want to strip the zeroes if they appear at the ends.
I know i can do this using if condition, but i want to know if there is any function made to do this efficiently than using if-else.
Example-
String = 0100010101010
Output = 10001010101
Also, i don't think using regex is any more efficient, complexity wise.
Try this:
s = "0100010101010"
print(s.lstrip("0").rstrip("0"))
'10001010101'
This should work for the string s:
s = s.strip("0")
Make sure s is a string and not a number.
Can you try this , it will work
s = str(s).strip("0")

get certain word from string that located between undercore, [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is one of the string that I got:
str ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
what I want:
['Name', 'coordinates','CITIZENS','colonisation']
I'm trying to get word in string such as Name, coordinate, citizens, colonisation with their original case.
I tried split method to remove underscores and make them individual word.
,but it did not work well.
How can I do this?
Yo can use a regular expression for that:
import re
text ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
re.findall('_(\w*)_', text)
Note str is a built python function, don't use for variable names
A regex should do the trick:
import re
s = '_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
result = re.findall('_(\w+)_', s)

How to remove all characters from a string after two white spaces in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.

Categories

Resources