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]==' '):
Related
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')
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?
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.
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 3 years ago.
Improve this question
If I have a string:
str = "This is my string"
And I split it:
str.split (" ")
to obtain:
["This","is","my","string"]
I wonder how to concatenate it in order to obtain:
["This","is","my string"]
This isn't my exact problem. I'm parsing log files and need to figure out how to concatenate a certain part of each line after splitting it.
You can do something like below.
>>> "This is my string".split(" ", 2)
['This', 'is', 'my string']
>>>
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 6 years ago.
Improve this question
Is it possible to remove periods from the middle of a string (sentence), leaving the ending period?
The answers that I have seen, basically strip all of the periods.
Remove periods at the end of sentences in python
If I understand correctly, this should do what you want:
import re
string = 'You can. use this to .remove .extra dots.'
string = re.sub('\.(?!$)', '', string)
It uses regex to replace all dots, except if the dot is at the end of the string. (?!$) is a negative lookahead, so the regex looks for any dot not directly followed by $ (end of line).