This question already has answers here:
How to define a function that would check if a string have whitespaces after the sentence is finished?
(2 answers)
Closed 8 years ago.
I am trying to write a function to check for trail whitespace, but not to remove the spaces. but i have no idea of how to do that. can somebody teach me?
thank you
Using str.endswith:
>>> 'with trailing space '.endswith(' ')
True
>>> 'without trailing space'.endswith(' ')
False
Related
This question already has answers here:
How to fix syntax error when printing a string with an apostrophe in it? [closed]
(6 answers)
Closed 3 years ago.
bob = input('How old are you? ')
print('You're', bob)
It's giving me syntax error because im using ' for you're. Whats the correct way of handling sentences with ' in them?
There are at least two ways to do this:
Use " for your string: "You're".
Escape the single quote: 'You\'re".
This question already has answers here:
What exactly is a "raw string regex" and how can you use it?
(7 answers)
Closed 6 years ago.
I am trying to remove tags in text that are identified by a backslash. For example, for the phrase 'Hello \tag world', I'd like to return the phrase 'Hello world'. I've tried the following but it doesn't get rid of the '\tag'.
print re.sub('\\[A-Za-z]+',' ',text)
I'm sure it's something simple, but I can't seem to figure it out.
Thanks for any help you can give!
Must be:
re.sub('\\\\[A-Za-z]+',' ',text)
Otherwise, '\\' is treated as a regex special escape character.
This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I'm trying to split
<team>
into just team, here is the code I'm using:
s = "<team>"
s.split(">")[1]
s
'<team>'
s.split(">")[1].split("<")[0]
s
'<team>
As you can see, it's still leaving me with
<team>
anyone know why>
str.split() function returns a list, it does not split the string in place.
You'll need to make a new variable:
s = "<team>"
t = s.split(">")[1]
t
This question already has answers here:
Error in regex to catch special characters
(2 answers)
Closed 6 years ago.
Can someone explain the following result?
Input to python 2.7.12 shell
re.match('[!##$%^&*()-_=+\|\[{\]};:\'<,.>/?~`]+', '2222').group()
Output:
'2222'
I don't understand why digits match this expression.
re.match('[!##$%^&*()-_=+\|\[{\]};:\'<,.>/?~`]+', '2222').group()
# ^^^
)-_ inside the brackets is a character range, and 2 is in that range.
This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Understanding slicing
(38 answers)
Closed 8 years ago.
In Python: How do I write a function that would remove "x" number of characters from the beginning of a string?
For instance if my string was "gorilla" and I want to be able remove two letters it would then return "rilla".
OR if my string was "table" and I wanted to remove the first three letters it would return "le".
Please help and thank you everyone!
You can use this syntax called slices
s = 'gorilla'
s[2:]
will return
'rilla'
see also Explain Python's slice notation