Having problem in this very basic PYTHON program [duplicate] - python

This question already has answers here:
Efficient way to add spaces between characters in a string
(5 answers)
Closed 1 year ago.
So i am making a very simple python program which takes a string as input from user and adds a '-' between every character but the problem is it adds it to last one too but i don't want that...
Here's the code:
string = input("Enter the string to be traversed: ")
for ch in string:
print(ch , end = "-")
Output:
Enter the string to be traversed: helloworld
h-e-l-l-o-w-o-r-l-d-
I don't want it to print '-' after the last character...

This is a common problem and there is a string method to deal with it. Instead of a for loop, join the characters with a dash.
string = input("Enter the string to be traversed: ")
print("-".join(string))
.join will work with anything that iterates strings. In your case, a string does that, character by character. But a list or tuple would work as well.

Related

String Replace in python by index wise or by one direction from start to end [duplicate]

This question already has answers here:
Index-wise Replacement of String Data in Python
(1 answer)
String replace in Python, in sequence, by index [duplicate]
(1 answer)
Closed 1 year ago.
I'm a newbie who just Started learning Python from YouTube. I am trying to make a program to replace old string Numbers with new string Numbers and facing problems while replacing numbers. Want to replace index-wise (What is its technical term (I don't know)). It can go in one direction or index-wise.
my string is = (001001001001001001001001001001001001001101100100110110011011001101011010011010110011011)
and I want to replace 101 with 01, 1101 with 11, 1001 with 011, and 11001 with 111,
so my replaced string/output string will be like this..
(00011000110001100011000110001100110110011011010110101100110111011)
As per python's normal string replace method it Cant work Anyone can help my
string = "001001001001001001001001001001001001001101100100110110011011001101011010011010110011011"
string = string.replace('101', '01').replace('1101', '11').replace('1001', '011').replace('11001', '111')
fin.close()
fin = open("2x.txt", "wt")
fin.write(string)
fin.close()
(00011000110001100011000110001100110110011011010110101100110111011)
In general python you can't "edit" strings, you need to create new ones. E.g:
my_old_string = '01010110110111011110111101111011110101101101101011011011010101010101010101011101110101110111101'
# use .replace()
my_new_string = my_old_string.replace('010', '0')
You could achieve the same thing with a single variable aswell:
string = '01010110110111011110111101111011110101101101101011011011010101010101010101011101110101110111101'
string = string.replace('010', '0')
string = string.replace('1101', '11')
# continue with this as often as you want
I am not sure, if your "doing all in one line" syntax is valid

Seperate each character in a string and put it in a list [duplicate]

This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 5 years ago.
How would you go about separating each character in a given input and turn it into a list?
For example I have
import string
print ("Enter string")
x = input("")
Enter string
The quick brown
I want the end result to be
['T','h','e',' ','q','u','i','c','k',' ','b','r','o','w','n']
Y'know, to turn every character as a separate string in a list instead of every word as a separate string.
Thanks!
Simply use list(x) where x is the string.

Removing all numeric characters in a string, python [duplicate]

This question already has answers here:
Removing numbers from string [closed]
(8 answers)
Closed 5 years ago.
Im looking to remove every single number in a string. More specifically, i'm looking to remove all numbers in the following codes string
Comp_String = "xxf1,aff242342"
how can one do this. (Obviously inside the code). I have found many answers to questions about removing the actual parts of the code that are letters but not numbers. Please explain aswell what your code is actually doing
You can find the answer here
Removing numbers from string
From this answer:
comp_string = "xxf1,aff242342"
new_string = ''.join([i for i in comp_string if not i.isdigit()])
It creates a new string using .join from a list. That list is created using a list comprehension that iterates through characters in your original string, and excludes all digits.
This will remove any characters that ARE NOT letters, by going through each character and only adding it to the output if it is a letter:
output_string = ""
for char in Comp_String:
if char.isalpha():
output_string = output_string + char
This will remove any characters that ARE numbers, by going through each character and only adding it to the output if it is not a number:
output_string = ""
for char in Comp_String:
if not char.isdigit():
output_string = output_string + char
You can do it with regular expressions using the https://docs.python.org/3.6/library/re.html module. The only regex you need is \d, which notates digits.
from re import sub
comp_string = "xxf1,aff242342"
print(sub(pattern=r"\d", repl=r"", string=comp_string))

I want to remove multiple characters from a string input by user [duplicate]

This question already has answers here:
Removing non numeric characters from a string
(9 answers)
Closed 6 years ago.
I am trying to ask the user for their phone number, and many people type their number such as "123-456-7890", "(123)456-7890".
I want to make the program strip the " ()- " from the input so that when I print the number back to the user, it shows it as 1234567980 without all the extra characters.
So far I have been able to remove only the first parentheses from the string by doing this:
number = str(input("Enter phone number: "))
print(number.strip('('))
Strings are iterable, so your problem can be efficiently solved using a list comprehension.
digits = '0123456789'
phone_number = ''.join([x for x in input("Enter phone number: ") if x in digits])
The benefit of this approach is that ONLY digits get included in the final result. Whereas with the replace approach you have to specify each and every exclusion.
Maybe not the most elegant way to do it but:
print(number.replace('(', '').replace(')', '').replace('-', ''))

Capitalize first letter in string if first character is not letter? [duplicate]

This question already has answers here:
python capitalize first letter only
(10 answers)
Closed 9 years ago.
I'd like to capitalize the first letter in a string. The string will be a hash (and therefore mostly numbers), so string.title() won't work, because a string like 85033ba6c would be changed to 85033Ba6C, not 85033Ba6c, because the number seperates words, confusing title(). I'd like to capitalize the first letter of a string, no matter how far into the string the letter is. Is there a function for this?
Using re.sub with count:
>>> strs = '85033ba6c'
>>> re.sub(r'[A-Za-z]',lambda m:m.group(0).upper(),strs,1)
'85033Ba6c'
It is assumed in this answer that there is at least one character in the string where isalpha will return True (otherwise, this raises StopIteration)
i,letter = next(x for x in enumerate(myhash) if x[1].isalpha())
new_string = ''.join((myhash[:i],letter.upper(),myhash[i+1:]))
Here, I pick out the character (and index) of the first alpha character in the string. I turn that character into an uppercase character and I join the rest of the string with it.

Categories