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)
Related
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.
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")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to keep a certain character for my python project which and I don’t want to replace every unused character with ‘’ is there any way to do it?
You can use a function re.sub in re library
For example:
data = re.sub('0123456789abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNNM+/-', '', data)
This will keep every character in the first parameter replace with the second parameter by using String from the third parameter
if you want to keep "d" in the string
origin = "abcdefghidx"
result = "".join([c for c in origin if c=="d"])
You can use str.replace('what to want to delete', 'what you want to add').
ex-
name = "stackoverflow"
newName = name.replace('o', '0')
newName becomes 'stack0verfl0w'
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)
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
I have a string containing a number, starting with x, for example, "x270" or "x9". It always starts with x. I need to get the number.
I'm doing this:
blatz = "x22"
a1 = int(re.search("x(\d+)", blatz).group(1))
This doesn't seem very Pythonic. I would welcome more elegant solutions.
Using re library seems to be an overkill. You don't have to search for a pattern, because you're saying that each string starts with x.
So you simply can do slicing:
blatz = "x22"
a1 = int(blatz[1:])
If you need further checks, you can look at str.startswith(), str.endswith and/or str.isdigit().
While slicing looks very pythonistic, there is also the possibility to use other string methods that lead to the same goal:
blatz = "x22"
a2 = int(blatz.lstrip("x")) # strip "x" from the left
a3 = int(blatz.partition("x")[-1]) # get everything after "x"
a4 = int(blatz.replace("x", "")) # replace every "x" with empty string
...
But slicing is faster and nothing unusual for Python programmers.
Please check out this.
import re
blatz = "x22"
print(re.search("\d", blatz).group())