Is there any method to replace space with multiple characters? [closed] - python

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
Example:
Input : "Base derived derived You are great"
Output: "Base->derived:derived You are great"
Here, the first two spaces are replaced with -> and : respectively, the rest of the string remains the same.

Nothing fancy, but if it's a one-off application, this would do the trick, using the Python str.split() method to create a list, splitting the string into three chunks on the first two spaces, then create a new string with those three chunks separated by '->' and ':'.
your_string = "Base derived derived You are great"
split_string = your_string.split(maxsplit=2)
result = f"{split_string[0]}->{split_string[1]}:{split_string[2]}"
As suggested in comments by #yatu, this can be reduced to a single statement using the *-operator to unpack the list:
result = '{}->{}:{}'.format(*your_string.split(maxsplit=2))
result in both cases:
'Base->derived:derived You are great'

Related

Destroy 2 caracters after a number [closed]

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 11 months ago.
Improve this question
I need help, in a long string, I want to know the the 2 characters after a number
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"
If I understand your question correctly, what you need is regex:
import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)
Output
gz15123da1az41sd23f168zgre4r6z
Explnatation
The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).

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.

A folder contains files of dog breeds like `Boston_terrier_02303.jpg`. I want to remove the numeric parts and also the `_`. How do I achieve this? [closed]

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 4 years ago.
Improve this question
My folder contains .jpg files in folder. I need to fetch only the characters from the file names.
I removed all the non alphabets but it resulted in a single string without spaces
Input: Boston_terrier_02303.jpg
Desired Output: Boston terrier
Assuming that you always have the same structure (n word fragments, 1 number, and the output), you can simply get your desired result by:
new_string = " ".join(string.split("_")[:-1])
To elaborate:
You start by splitting the strings at the underscores, and then selecting everything but the last. Then simply join the remaining strings with a space between them.

Python opposite of strip() [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 years ago.
Improve this question
I'm trying to figure out a function that does the opposite of strip for my Python class.
The exact question is:
What function can be used to surround a string with spaces in order to make it a certain length?
Thanks!
The "opposite" of strip is the center method:
>>> 'Hello, World!'.center(30)
' Hello, World! '
You can specify the fill character to use as second argument. The default is whitespace.
There are also ljust and rjust that will left-justify and right-justify the text up to a certain length, and as such could be considered "opposites" of lstrip and rstrip.
format strings
"%50s World"%("Hello")
"%-50s World"%("Hello")
"%^50s World"%("Hello")
or newfangled
"{0:50s} World".format("Hello")
"{0:>50s} World".format("Hello")
"{0:^50s} World".format("Hello")
these have nifty side effects
"%^*s World"%(50,"Hello")
Try s.center(N) where N is the line length you want, s - your string.
For example:
>>> "xx".center(10)
' xx '

Categories

Resources