Multi-line Strings [closed] - python

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

Related

How to remove characters that repeat more than twice in a row/together in a string using 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 7 months ago.
Improve this question
How can we reduce a string like haaaaaaapppppyyyyyy to haappyy
Such that repetition is allowed to a maximum of twice in a row for a character in a string?
including any character ( special characters also )
converting --------------------- to --
We can use a regex replacement:
inp = "haaaaaaapppppyyyyyy"
output = re.sub(r'(\w)\1{2,}', r'\1\1', inp)
print(output) # haappyy
The above logic matches any one character which is followed by itself two or more times. It then replaces with just two of the character.

Easy way to extract multiple dates from string without spaces? [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
I am looking for a way to automatically extract dates from a string, but following each other without a delimiter
For example my string is: \n-\n24-04-201923-04-201922-04-201921-04-201920-04-201919-04-201918-04-2019
How can I get this output:
24-04-2019
23-04-2019
22-04-2019
21-04-2019
20-04-2019
19-04-2019
18-04-2019
Any help would be appreciated!
Given that they're all of equal length, you can just clear the \n's then use textwrap:
import textwrap
print(textwrap.wrap(my_string, 10))
You can remove \n's using strip():
my_string = my_string.strip()
You can use this code also.
string='\n-\n24-04-201923-04-201922-04-201921-04-201920-04-201919-04-201918-04-2019'
newStr=string[3:]
for char in range(0,len(newStr),10):
print newStr[char:char+10]
Here's the output
24-04-2019
23-04-2019
22-04-2019
21-04-2019
20-04-2019
19-04-2019
18-04-2019

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']

Python: remove characters from a string? [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 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'

How do I write a function to escape all double quotes in a string in Python? [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 8 years ago.
Improve this question
How can I replace all the " with the raw string \" in a string such as "Hello", said he. in Python?
s = '"Hello", said he.'
print s.replace('"', r'\"')
# output
\"Hello\", said he.
It helps to use the r'' notation to indicate that the string should be raw and not interpreted. Helps with backslashes.
Use replace().
>>> print '"Hello"'.replace('"', '\\"')
\"Hello\"

Categories