Reversing and returning a string [duplicate] - python

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 5 years ago.
I am trying to write a function that takes a string and reverses the words in that string, and then returns the string. I have written this code but when I execute it nothing happens. A blank appears.
def reverse(tex):
new_w = " "
for i in range(0, len(tex)):
new_w += text[len(tex) - 1 - i]
return new_w

>> name = "pawan"
>> name[::-1]
>> "nawap"
This is better, isn't it?
This is called slicing.
Here's the syntax for slicing:
[ first char to include : first char to exclude : step ]
EDIT: I suggest you take a look at this link.

Related

Is there any way to count the number of times a string is present in another given string? [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
I tried to create a program which returns the number of times a certain string occurs in the main string.
main_string="ABCDCDC"
find_string="CDC"
print(main_string.count(find_string))
Output=1
....
But there are 2 CDC. Is there any other ways to solve?
Try using regex:
print(len(re.findall(fr"(?={find_string})", main_string)))
Or try using this list comprehension:
x = len(find_string)
print(len([main_string[i:x + i] for i in range(len(main_string)) if main_string[i:x + i] == find_string]))
Both codes output:
2

How do I reverse an index number into the character it represents in Python? [duplicate]

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 1 year ago.
I have a code in which I am attempting to reverse a certain string. I have received instructions to only use index to do this, so I used index, and came to a halt when I found this. Here is my code first, for context.
emptyList = []
Len = len(var)
for i in range(Len, 0, -1):
emptyList.append(i)
print(emptyList)
My problem is that whenever I print the variable emptyList, I only get a list of numbers, like such:
[3, 2, 1]
I want the numbers to represent their character, like 3 = G, 2 = O. How do I do that?
Use emptyList.append(var[i-1]) not just i.

Python comparing a list with set to find pangram [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 3 years ago.
I'm trying to find if a string is a Pangram. My approach is to get the string to have unique letters by using the set method. Then using the string.ascii as the base alphabet. I find out after some testing that if I try to compare the 2 with the 'in' operator. Some of the letters get passed over and won't be removed from the alphabet list.
def is_pangram(sentence):
uniqueLetters = set(sentence.lower().replace(" ", ""))
alphabet = list(string.ascii_lowercase)
for letter in alphabet:
if letter in uniqueLetters:
alphabet.remove(letter)
if len(alphabet) <= 0:
return True
return False
print(is_pangram("qwertyuiopasdfghjklzxcvbnm"))
this example will compare 13 letters and the rest will not. Anyone can point me in the right direction? Am I misunderstanding something about set?
Perhaps the following does what you want:
import string
target = set('qwertyuiopasdfghjklzxcvbnm')
all((k in target for k in set(string.ascii_lowercase)))

String to Int in Python [duplicate]

This question already has answers here:
Why does map return a map object instead of a list in Python 3?
(4 answers)
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 5 years ago.
How to Convert a string like '123' to int 1,2 and 3 so that I can perform 1+2+3. I am new to python. Can you please help me? I am not able to split the list. I don't think splitting the string will be of any use as there are no delimiters. Can you help me to understand how can this string elements be separated and treated as intergers?
x = "123"
s = 0
for a in x:
s = int(a) + s

Padding zeroes to left of string (python)? [duplicate]

This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 7 years ago.
The length of the string needs to be 5 characters. When the string is "1" it needs to be returned as "00001", when the string is "10" it needs to be returned as "00010" and so on. I'm wondering how to do this using loops?
If you want to use for-loops, you can solve the problem like so:
def addPadding(str):
output = ''
# Prepend output with 0s
for i in range(5 - len(str)):
output += '0'
output += str
return output
print(addPadding('10'))
>> 00010
print(addPadding('1'))
>> 00001
If you can't use string formatting or arrays or anything besides integer operators, you should be able to figure it out using division and a loop.
Is 10 divisible by 10000?
Is 10 divisible by 1000?
Is 10 divisible by 100?
etc.
Try typing 10/10000 in your python interpreter. What's the result? :)

Categories