4

Here's my code:

start_j = raw_input('Enter a name: ')
start_j = start.replace("A", "J")
start_j = start.replace("B", "J")
start_j = start.replace("C", "J")
print "Your name is " + start_j

Is there anyway to put all the alphabets in one list so that I wouldn't have to repeat the same process again and again until I reach letter "Z" I tried using loops, but I still can't seem to get the right way to do it.

Here's a scenario: The user will be prompted to input a name. If the name contains a letter other than "J", it will be automatically replaced using the replace() function. Hence it will print out the input starting with J

Here's an example:

site = raw_input('Enter your website: ')
site = site.replace("http://", "")
site = site.replace("https://", "")
site = site.replace("ftp://", "")
print "Your website is: " + site

An expected input would be http://www.google.com So the expected out would become:

Enter your website: http://www.google.com
Your website is: www.google.com

I'm looking for a way to put "http://", "https://", "ftp://" all in one list so I wouldn't have to enter

site = site.replace("something", "something)

many times

4
  • 2
    Post some sample input and output. It'll clear things up. Commented Dec 24, 2012 at 0:17
  • Oh wow, your edit really changes the question a lot. Your original question was far from clear enough. Commented Dec 24, 2012 at 0:26
  • Sorry if I was unclear. It's 5 am in the morning and I haven't slept in a while. Commented Dec 24, 2012 at 0:29
  • @MarkByers, It seems like the same question to me before vs after the sample/example/illustration. Can you help me figure out what or how i was reading the question wrong before the sample if you understood it differently? I ask this because i am working on bettering my requirement discerning skills, to better read a given requirement document or may be even trying to look at them from a different perspective. This may sound weird but i promise you it is not and it really matters in work env and especially for me. ty. Commented Oct 28, 2021 at 17:34

5 Answers 5

4

You could use a regex to replace all of the letters at once:

>>> import re
>>> re.sub(r'[A-Z]', 'J', 'This Is A Test Name')
'Jhis Js J Jest Jame'

(After edit): You can use .startswith() and string slicing:

>>> name = 'A Name'
>>> 
>>> if not name.startswith('J'):
...     name = 'J' + name[1:]
... 
>>> name
'J Name'

Although I'm not sure why you'd even need to check with .startswith(). Either way, the result will be the same.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use this:

remove_from_start = ["http://", "https://", "ftp://"]
for s in remove_from_start:
    if site.startswith(s):
        site = site[len(s):]
        break

Or a regular expression based solution:

import re
regex = '^(https?|ftp)://'
site = re.sub(regex, '', site)

2 Comments

start_j is variable that requires an input which would be the name. If any name starts with a letter other than "J", it will be automatically replaced.
@MaxWayne: Starts with? replace examines the entire string, not just the first character.
1
import re

site = raw_input('Enter your website: ')
# input http://www.google.com or https://www.google.com or ftp://www.google.com
site = re.sub('^(?:https?|ftp)://', '', site)
print "Your website is: " + site

Comments

0

use a dictionary:

In [100]: import string

In [101]: dic=dict.fromkeys(string.ascii_uppercase,"J")

In [104]: start_j = raw_input('Enter a name: ')
Enter a name: AaBbCc

In [105]: "".join(dic.get(x,x) for x in start_j)
Out[105]: 'JaJbJc'

Edit:

In [124]: dic={"https:":"","http:":"","ftp:":""}

In [125]: strs="http://www.google.com"

In [126]: "".join(dic.get(x,x) for x in strs.split("//"))
Out[126]: 'www.google.com'

Comments

0

use re, dict and lambda:

import re
replacte_to = {
    "http://": "",
    "https://": "",
    "ftp://": "",
}
re.sub("^(ht|f)tps?://", lambda match: replacte_to[match.group(0)], YOUR_INPUT_STRING)

Comments

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.