4
#!/usr/bin/python
import sys,re
import subprocess

def funa():
        a = str(raw_input('Enter your choice [PRIMARY|SECONDARY]: ')).upper().strip()
        p = re.compile(r'.*'+a+'.*')
        result = p.findall(a)
        str1 = ''.join(result)
        print str1
funa()

I have above code in test.py file and When I run this code and give my choice as SECONDARY, I am getting the output SECONDARY as string:

[oracle@localhost oracle]$ ./test.py Enter your choice [PRIMARY|SECONDARY]: SECONDARY SECONDARY

I want to add prefix as '^' and suffix as '$' to add in my output. Which should be a string only. Means I want to get the output as :

^SECONDARY$

Please let me know how can I achieve this.

1
  • You are already using + on strings earlier in your function. What is it you're having trouble with? Commented Nov 26, 2015 at 14:52

3 Answers 3

11

You can concatenate those onto the string using +.

For example (for Python 2):

print "^" + str1 + "$"

Or without + using f-strings in Python 3:

print(f"^{str}$")

Or if you want to add a prefix to every string in a list:

strings = ['hello', 1, 'bye']
decoratedstrings = [f"^{s}$" for s in strings]

result:

['^hello$', '^1$', '^bye$']
Sign up to request clarification or add additional context in comments.

2 Comments

@barny thanks it worked for me. Not sure why its downgrade.
Though I did not downvote, I guess the downvoter expects a bit of explanation about the code, something other than try this.
1

Bit out of context but can be helpful for people like me.

If a string list then,

strString = map(lambda x: '^' + x + '$', strString)

The resultant will be string list as well with prefix and suffix added to each string element of the list.

2 Comments

Rather than map() and lambda, wouldn't it be simpler to use a comprehension like strings=[f"^{s}$" for s in strings]?
@DisappointedByUnaccountableMod, better answer there!
-1
$server = "name"

[string[]]$array_white_list = Get-Content -Path "$home\MonTool\whitelist.txt"

Set-Content -Path "$home\MonTool\signaleringen.txt" -Value "Server: $server"
Add-Content -Path "$home\MonTool\signaleringen.txt" -Value "Datum/Tijd: $datum_tijd"

Add-Content -Path "$home\MonTool\signaleringen.txt" -Value $proces_naam

Write-Warning 'De opgegeven servernaam is niet bekend; het script wordt gestopt.'

Write-Host "Datum/Tijd: $datum_tijd"
Write-Host "Het script is klaar met de uitvoering. Tot de volgende keer."

1 Comment

Your answer could be improved with some explanatory text, it will make it more useful to others who may land here in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.