switching between different quotation marks for output - python-3.x

how can i get my code to change the quotation marks of my output. i saw some references that mentioned json, but i think i need to write it myself.
so i'll post the question and then my code:
Program: quote_me() Function
quote_me takes a string argument and returns a string that will display surrounded with added double quotes if printed
check if passed string starts with a double quote ("\""), then surround string with single quotations
if the passed string starts with single quote, or if doesn't start with a quotation mark, then surround with double quotations
Test the function code passing string input as the argument to quote_me()
[ ] create and test quote_me()
def quote_me (word):
if word == ("\'"):
str(word).replace ("\'", '\"')
else:
return word
print (quote_me(input ("what is the sentence: ")))
maybe i've misunderstood what is required as well, if that's the case, please do tell.

def quote_me(word):
if word.startswith("\"") and word.endswith("\""):
word = word.replace("\"","\'")
elif word.startswith("\'") and word.endswith("\'"):
word = word.replace("\'","\"")
else:
word = "\""+word+"\""
return word

Related

If input() returns a string, why doesn't print() display the quotation marks?

I'm having trouble understanding the following:
According to my book, unless otherwise specified, input will return a string type. If a string is printed wouldn't you expect the quotes to be included in the result? Is this just how print() is designed to work, if so why?
Example problem:
x = input() # user enters 5.5
print(x) # i expect '5.5' to be printed, instead 5.5 is printed
Wouldn't it be better to print the variable x for exactly what it is?
No, you use quotes to create a literal string; quotes are not part of the string value itself. If you want to see the quotes, ask Python for the representation of your string, i.e.
print(repr(x))

Does a python string include the quotation marks?

For ('bobby'), is the string here 'bobby' or just bobby? I've tried to research into it but the other questions people ask are more complicated. I only want to know whether a full python string includes or doesn't include the '' marks.
If you are declaring a string, you need the quotation marks, like this example:
a = "Hello"
However, if you are just talking about the string itself, the quotations are not part of it. If I were to print variable a that I declared above, the output would be Hello, not "Hello".
print(a) -> Hello
A string is enclosed within the quotation mark, it does not mean that quotations are included in the string. The quotations are given just to tell the compiler that it is a string data type.
Ex -> "Hello"
'Hello'
But if you include double or single quotes inside single or double quotes in python respectively, then the inner quotation will be treated as a string.
Ex -> 'Ram said, "I love apples."'
"Ram said, 'I love apples.'"

How to use .replace() properly

I am trying to remove "--" from words. For example, the word "World--fourthousand" should be replaced with white space doing string.replace('--', ' ')
String.replace('--', ' ') I have already tried and doesn't remove it.
if "--" in string:
string.replace("--", " ")
Expected "World fourthousand"
Actual "World--fourthousand"
replace returns a new string with the substring replaced, it doesn't alter the original string. So you could do:
string = string.replace("--"," ")
The replace() function should return a copy of the string with the intended characters replaced. That being said, I believe you should assign the result of the replace function into another variable, which will hold the desired value.
if "--" in string:
new_string = string.replace("--", " ")
Strings are what you call immutable objects, which means that whenever you modify them, you get a new object, and the original string is intact, so you would need to save the edited string in a new variable like so:
s = "World--fourthousand"
s = s.replace("--"," ")
print(s)
#World fourthousand
From the docs: https://docs.python.org/3/library/string.html#string.replace
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.

Removing special characters from a string In a Groovy Script

I am looking to remove special characters from a string using groovy, i'm nearly there but it is removing the white spaces that are already in place which I want to keep. I only want to remove the special characters (and not leave a whitespace). I am running the below on a PostCode L&65$$ OBH
def removespecialpostcodce = PostCode.replaceAll("[^a-zA-Z0-9]+","")
log.info removespecialpostcodce
Currently it returns L65OBH but I am looking for it to return L65 OBH
Can anyone help?
Use below code :
PostCode.replaceAll("[^a-zA-Z0-9 ]+","")
instead of
PostCode.replaceAll("[^a-zA-Z0-9]+","")
To remove all special characters in a String you can use the invert regex character:
String str = "..\\.-._./-^+* ".replaceAll("[^A-Za-z0-1]","");
System.out.println("str: <"+str+">");
output:
str: <>
to keep the spaces in the text add a space in the character set
String str = "..\\.-._./-^+* ".replaceAll("[^A-Za-z0-1 ]","");
System.out.println("str: <"+str+">");
output:
str: < >

Python - exclude data with double quotes and parenthesis

I have a list which contains set of words in single quotes and double quotes, now i want to grep all the data only in single quotes.
Input_
sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
Output:
telco_name_list.event_date,reference.date,data_flow.code
I want to exclude the parenthesis and string in double quotes.
Since you tagged the post as python-3.x, I'm assuming you want to write python code. Based on your example this would work:
#!/usr/bin/env python3
Input_sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
comma = False
result = []
for i in range(len(Input_sentence)):
e = Input_sentence[i]
if e.startswith("'"): continue
e = e.replace('(', '').replace(')', '')
if comma:
print(",", end="")
comma = True
print(e, end="")
print()
This code iterates through the list, ignoring elements which begin with a single quote and filtering out parentheses from elements before printing them on stdout. This code works for the given example, but may or may not be what you need as the exact semantics of what you want out are somewhat ambiguous (i.e. is it fine to filter out all parentheses or just the ones at the beginning and end of your elements?).

Resources