tips needed to write the function quote this - python

Write a function called "quote_this" that accepts two
arguments: a string representing a quote (not surrounded
by quotation marks) and a string of a name. The function
should return a new string with the quote surrounded by
quotation marks (") followed by a dash and the given
name in python.
don't know how to go about starting to solve this.any help

This should work:
def quote_this(quote,name):
return '"' + quote + '" - ' + name
In which quote represents the string of your quote and name the name of the author

def quote_this(quote_string, name):
return '\"' + str(quote_string) + '\"' + ' - ' + str(name)
quote_this('this', 'John')
You should really look into Google or read a book for this. It's really simple. Also, provide a sample code next time.

Related

how to have both string placeholder surrounded with quotes inside a string in python?

i have xpath that looks like this:
"//*[#id="comments_408947"]/div[2]/div[2]/div[2]"
the comment_408947 must be wrapped up with quotes as well as the entire xpath.
i have the number 408947 as a string and need to add it after 'comments_'
com_id = '408947'
query= f("//*[#id=comments_" + """%s""" + "]/div[2]/div[2]/div[2]", com_id)
but it's not working.
Use triple quotes, it will solve the issue for you or you need to escape the inner ", otherwise it is interpreted as a string endpoint.,
'''//*[#id="comments_408947"]/div[2]/div[2]/div[2]'''
"//*[#id="comments_408947"]/div[2]/div[2]/div[2]" is invalid - you need to use different quotes to allow " as inside characters - or you need to escape them:
'''"//*[#id="comments_408947"]/div[2]/div[2]/div[2]"''' # works - uses ''' as delim
# too complex for string coloring here on SO but works:
""""//*[#id="comments_408947"]/div[2]/div[2]/div[2]"""" # works - uses """ as delim
"//*[#id=\"comments_408947\"]/div[2]/div[2]/div[2]" # works - escapes "
'//*[#id=\"comments_408947\"]/div[2]/div[2]/div[2]' # works - uses ' as delim
the same goes for your id-insertion -you can use string interpolation:
com_id = '408947'
query= f'''//*[#id="comments_{com_id}"]/div[2]/div[2]/div[2]''', com_id)

rstrip(), lstrip(), and strip() in Python

I am using Sublime Text version 3.0 and my input is:
little_known_person = "' Christopher '"
print(little_known_person.strip())
but my output is ' Christopher ' instead of 'Christopher'. The output is the same if I try the rstrip() and lstrip() methods.
You need to strip leading/trailing whitespace and single quotes. The default, with no argument, is to strip only whitespace.
little_known_person.strip(" '")
The argument is an arbitrary iterable (str, list, tuple, etc) containing characters that should be stripped. The order doesn't matter.
You need to strip whitepaces and single quotes and then re-add the quotes:
little_known_person = "'" + little_known_person.strip(" '") + "'"
Please try if the following approach solves your issue:
>>> little_known_person = "' Christopher '"
>>> little_known_person.replace("'", "").strip()
'Christopher'

Can not add backslash into input parameter using Python

I need to escape all special character and then add back slash () into input parameters using python. I have done something but it's not working. My code is below.
rname = request.POST.get('rname')
ename = re.escape(rname)
add_slash = ename + '\'
Here I need to escape all special character first and then add the \ with that string.
A backslash also needs to be escaped, like so
add_slash = ename + '\\'
**Hello Satya, **
Your Solution
Try this below code,
rname = request.POST.get('rname')
ename = (re.escape(rname)) + "\\"
add_slash = ename
More knowledge about string literal so read below link,
https://docs.python.org/2.0/ref/strings.html
I hope my answer is helpful.
If any query so c

Replace the " character with a space

I have a CSV file, where each comma delimited field is enclosed in " - eg. "fred", "bert", "blah". I am trying to use the replace function but can't seem to have it recognize the " character. example, if the record is in a string called buffer:
buffer.replace('\"','')
Add space between double quotes
p = '"fred", "bert", "blah"'
p.replace('\"'," ")
' fred , bert , blah '
Why do you escape the double quotes if it's inside single quotes ?
Try the following :
a = '"my string"'
a = a.replace('"',' ')
print(a)
#=> ' my string '
You are not replacing it with space firstly, but with empty string
Try using buffer.strip("\"")

Using 'Replace' Function in Python

I have an access table that has a bunch coordinate values in degrees minutes seconds and they are formatted like this:
90-12-28.15
I want to reformat it like this:
90° 12' 28.15"
essentially replacing the dashes with the degrees minutes and seconds characters and a space between the degrees and minutes and another one between the minutes and seconds.
I'm thinking about using the 'Replace' function, but I'm not sure how to replace the first instance of the dash with a degree character (°) and space and then detect the second instance of the dash and place the minute characters and a space and then finally adding the seconds character at the end.
Any help is appreciated.
Mike
While regular expressions and split() are fine solutions, doing this with replace() is rather easy.
lat = "90-12-28.15"
lat = lat.replace("-", "° ", 1)
lat = lat.replace("-", "' ", 1)
lat = lat + '"'
Or you can do it all on one line:
lat = lat.replace("-", "° ", 1).replace("-", "' ", 1) + '"'
I would just split your first string:
# -*- coding: utf-8 -*-
str = '90-12-28.15'
arr = str.split('-')
str2 = arr[0] +'° ' + arr[1] + '\'' +arr[2] +'"'
print str2
You might want to use Python's regular expressions module re, particularly re.sub(). Check the Python docs here for more information.
If you're not familiar with regular expressions, check out this tutorial here, also from the Python documentation.
import re
text = 'replace "-" in 90-12-28.15'
print(re.sub(r'(\d\d)-(\d\d)-(\d\d)\.(\d\d)', r'''\1° \2' \3.\4"''', text))
# use \d{1,2} instead of \d\d if single digits are allowed
The python "replace" string method should be easy to use. You can find the documentation here.
In your case, you can do something like this:
my_str = "90-12-28.15"
my_str = my_str.replace("-","°",1)# 1 means you are going to replace the first ocurrence only
my_str = my_str.replace("-","'",1)
my_str = my_str + "\""

Categories

Resources