How to check for palindromes in python? [closed] - python

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
Can you write a method that checks whether a word or a phrase is a palindrome?
NOTE: Palindrome is a word which reads the same backwards. Eg = madam, racecar or phrases like "nurses run".

You can do it like this:
def palindrome(s):
s = s.replace(' ','') # This replaces all spaces ' ' with no space ''. (Fixes issues with strings that have spaces)
return s == s[::-1] # Check through slicing
palindrome('nurses run')

Related

Only print the date in 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 5 months ago.
Improve this question
This is my string
word = " Saturday Fortune 08-09-2022 (4872) Draw Numbers "
But I only want to print the date in the string like this
'08-09-2022'
So how can I achieve this?
Should use a regular expression to find that part in the string. Something like:
import re
match = re.search(r'\d{2}-\d{2}-\d{4}', word)
This already seems to be answered as a subproblem in this thread.

Remove words in string if it contains \u 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
I want to remove words in string if it contains \u in python?
ex:
string ="\uf064thickness cfoutside\uf0d7\uf03a\uf03d TC2019 45TRCMat"
The final output should be like this.
"TC2019 45TRCMat"
After removing all of the words if it contains \u.
Rather then looking to remove unicode character go the other way and only allow ascii character:
string ="\uf064thickness cfoutside\uf0d7\uf03a\uf03d TC2019 45TRCMat"
def is_ascii(s):
return all(ord(c) < 128 for c in s)
for s in string.split(" "):
if is_ascii(s):
print(s)
Reference: How to check if a string in Python is in ASCII?

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.

Detect double spaces at start of sentence [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
If a sentence starts with double spaces, I need to neglect that line and check with other lines. If it doesn't, I need to print that line. I am newbie to python, please help me out with this.
It is pretty simple. You can check if the first char of the string is . See below example
sentences = [' this wont print', 'this will print', " this wont print either", " this will print"]
for sentence in sentences:
if sentence.startswith(' '):
print(sentence)
You can extract the first two characters of the string S using S[0:2] and check if it is equal to double spaces ' ' or not.
if (S[0:2]==' '):

Regex in python to catch a string like MMS-2839 [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 years ago.
Improve this question
How can I catch some string like these with a regex in python?
M1Sxs-2839
McS-28S9213
Both the first and the second part (divided by the -) can contains letters and numbers (case insensitive).
You may try the below re.match function.
re.match(r"(?i)[A-Z0-9]+-[A-Z0-9]+$", st)
(?i) helps to do case-insensitive match. Since re.match scans the input from start, you don't need to add start of the line anchor ^ explicitly.

Categories

Resources