Python: remove characters from a string? [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 8 years ago.
Improve this question
I took a python course back when i was in high school but now I barely remember anything about it. I'm bored today and though I should try some python exercises.
Example:
string = '3dc8uo8c33a v8c08oizl6ga'
The code needs to remove 3d 8u 8c ... ect
so that the
answer = 'coca cola'

Assuming the rule is "split the string along whitespace, then take every third letter of the words, and add them back together", you can use
>>> string = '3dc8uo8cc33a v8c08oizl6ga'
>>> " ".join("".join(s[2::3]) for s in string.split())
'coca cola'

Related

formating string and passing argument at runtime [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 2 years ago.
Improve this question
eg-In this instead of using 21 (a value), I want to use a variable to generalize it
print("{:-^21}".format(".|."*(2*(i+1)-1)))
I want to use something like this
print("{:-^M}".format(".|."*(2*(i+1)-1)))
That can easily enough be done. For example:
M = 40
i = 3
print("{val:-^{width}}".format(width=M, val=".|."*(2*(i+1)-1)))
Outputs:
---------.|..|..|..|..|..|..|.----------
You could also do it with f-strings (note the outer ' because " is used on the inner expression):
print(f'{".|."*(2*(i+1)-1):-^{M}}')

get everything in a string after a specific symbol in python using regex [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 4 years ago.
Improve this question
Hi I would like to get everything after the '_' using regex
For example: I have --> I want
'aaa_bbb_ccc' --> 'bbb_ccc'
'dd_aaaa_1' --> 'aaaa_1'
'*/_2d*_//' --> '2d*_//'
Is there anyway to do it?
Thanks in advance.
I rather like the split suggestion given by #Maroun in a comment above. Here is an option using re.sub:
x = "aaa_bbb_ccc"
output = re.sub(r'^[^_]+_', '', x)
print(output)
bbb_ccc
The regex does not require much explanation, and it just removes all content up to, and including, the first underscore in the input string.

How to remove characters from a list. [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 4 years ago.
Improve this question
I have a list output:
['Go497f9te(40RAAC34)\n','G0THDU433(40RAAC33)\n']
and I want to clean it up in order to output:
[40RAAC34,40RAAC33]
If you have a string:
'hello (world)'
and want the text between the brackets, you can either use a regex:
import re
re.findall('\((.*?)\)', s)[0]
#'world'
or, if you are sure that there is only one set of brackets (i.e. no leading ) chars) then you can just use slicing:
s[s.index('(')+1:s.index(')')]
#'world'
So then you just need to throw this into a list-comprehension or similar.
l = ['Go497f9te(40RAAC34)\n','G0THDU433(40RAAC33)\n']
[s[s.index('(')+1:s.index(')')] for s in l]
#['40RAAC34', '40RAAC33']

Convert input letter to different letter [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 7 years ago.
Improve this question
I am new to python (pardon my bad terminology). I cannot find a solution to my problem.
I am trying to make a simple encryption system. I want to convert the characters of user input to specific characters. For example: ABC would turn into ZYX.
Can anyone help me with this? Thanks.
assuming you just want a simple substitution cipher you can use the translate function:
# in python3:
# table = str.maketrans('ABC', 'ZYX')
# in python2:
from string import maketrans
table = maketrans('ABC', 'ZYX') # add the rest of the alphabet and the desired
# subsitutions
print('CBA'.translate(table))
# output: 'XYZ'

Multi-line Strings [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Suppose, I want to define a two-line (or a multi-line) string.
I can do this in two ways:
Using escape sequence for the newline character.\n
Ex: "This is the first sentence. \n This is the second sentence."
Using triple-quoted strings.
Ex: """ This is the first sentence.
This is the second sentence."""
Which is the more efficient or conventional ? Why ?
I'm tempted to say it doesn't matter since each one still scans inside for escaped characters while parsing the text.
>>> print "a\n\tb"
a
b
>>> print """a\n\tb"""
a
b

Categories