I made a function that returns a string as shown below but I was wondering how do I keep the double quotes for the quote?
def quote_maker(quote, name, year):
''' (string, string, number)-> None
Returns a sentence displaying in what given year the
given name of a person said the given quote.
'''
return (' In '+ str(year) +', a person called '+ name +' said: '+ quote)
For example my function returns:
quote_maker("Everything should be made as simple as possible but not simpler.", "Albert Einstein", 1933)
'In 1933, a person called Albert Einstein said: Everything should be made as simple as possible but not simpler.'
Instead of: (with double quotes)
'In 1933, a person called Albert Einstein said: "Everything should be made as simple as possible but not simpler."'
You can escape characters (done with \) that have a special meaning to treat them as characters:
" this is quoted: \"example\" "
represents the string:
this is quoted: "example"
I think best way is to use string formating. And return the variable.
sentence = 'In {0}, a person called {1} said: "{2}".'.format(year, name, quote)
For complicated string you should use escape characters. For more details enter link description here
Related
I'm trying to replace all single quotation marks with double quotation marks in Clojure. i.e. if the string id " 'name' " I want it to become " "name" ". How can I do that?
I've been trying to use the replace function like this:
(clojure.string/replace " 'The color is red' " #"'" """)
but it is not working.
You've forgotten to escape the double quotation mark
(clojure.string/replace " 'The color is red' " #"'" "\"")
I do this so often I made some convenience functions:
quotes->double
quotes->single
You can find the details here. You may also find this template project useful, esp. the documentation list at the end.
I have a string value with an apostrophe like this:
"I\\xE2\\x80\\x99m going now."
How can I get correct apostrophe value?
"I`m going now."
As you know, \xE2\x80\x99 is the a unicode character U+2019 RIGHT SINGLE QUOTATION MARK, but I have a string representation instead of byte...
Perhaps this is what you want:
utf8_apostrophe = b'\xe2\x80\x99'.decode("utf8")
str = "I"+utf8_apostrophe+"m going now"
Aside:
I ran into this when converting a single quotation mark, within a UTF-8-encoded tweet, into a normal single quote.
import re
original_tweet = 'I’m going now'
string_apostrophe = "'"
print re.sub(utf8_apostrophe, string_apostrophe, original_tweet)
which produces
I'm going now
I would like this to accept "John Smith" or "Smith, John". Currently this only accepts JohnSmith Smith,John etc. What is the issue?
while not str(NAME).isalpha():
NAME=input("Enter patient's name : ")
new_entry.append(str(NAME))
The isalpha method checks if the string is one word, e.g. no numbers and no spaces. For multiple words, you can use something like
while not all(s.isalpha() for s in NAME.replace(',', ' ').split()):
where the .replace(',', ' ') is what allows for treating spaces and commas on the same footing.
I have strings which looks like this [NAME LASTNAME/NAME.LAST#emailaddress/123456678]. What I want to do is parse strings which have the same format as shown above so I only get NAME LASTNAME. My psuedo idea is find the index of the first instance of /, then strip from index 1 to that index of / we found. I want this as a VBScript.
Your way should work. You can also Split() your string on / and just grab the first element of the resulting array:
Const SOME_STRING = "John Doe/John.Doe#example.com/12345678"
WScript.Echo Split(SOME_STRING, "/")(0)
Output:
John Doe
Edit, with respect to comments.
If your string contains the [, you can still Split(). Just use Mid() to grab the first element starting at character position 2:
Const SOME_STRING = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(Split(SOME_STRING, "/")(0), 2)
Your idea is good here, you should also need to grab index for "[".This will make script robust and flexible here.Below code will always return strings placed between first occurrence of "[" and "/".
var = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(var, (InStr(var,"[")+1),InStr(var,"/")-InStr(var,"[")-1)
How do you insert a "tab" in a string? I thought it was t enclosed in <> , but I do:
'Name <t> Age <t> Occupation'
prints exactly how it's typed. I would like to get
Name Age Occupation
instead of
Name <t> Age <t> Occupation
'Name ', String tab, ' Age ', String tab, ' Occupation'
If String does not understand #tab, you have to :
'Name ', Character tab asString, ' Age ', Character tab asString, ' Occupation'
As Damien noted, you can simply concatenate several calls to String tab to achieve what you wanted. The <t> trick you're trying, though, happens in Squeak-based Smalltalks if you call expandMacros on your string. E.g.,
'Here is<t>a tab and<n>a blank line' expandMacros
This mechanism is generic, and quite easily extensible; see String>>expandMacrosWithArguments: for more information.