Count letters in sentences (Python) - python-3.x

i'm a beginner programmer, and i want to ask about count the many letters in a sentence.
For the example program like this :
data = "Hello World"
s = input() # Try to input L
Output :
L = 3
So the output is just what i input, not with other letters like w, o, r, d, h, e. I wrote some code but i dont know why the output sometimes None or 11 or 0. Here the code i write with output 0
data = "Hello World"
s = input()
sum = 0
for s in data :
if s == data :
sum += 1
print(sum)
Any suggestion for what i can do to write the program like i want ?

If I am not wrong, what I understood from your problem is that you wish to count the total number of times an alphabet appears in a string and then print the total number of times it occurred in the sentence.
In order to achieve this, I can show you two methods:
Method 1:
Naive approach:
data="Hello World"
s=input() # Assume you put 'l'
count = 0
for i in data:
if i == s:
count = count + 1
print(count)
Doing so, you will get output as 3
Method 2:
Using count():
data="Hello World"
counter = data.count('l')
print("Count of l in data is : " + str(counter))
Hope this answered you query.

Related

Issue with ASCii in Python3

I am trying to convert a string of varchar to ascii. Then i'm trying to make it so any number that's not 3 digits has a 0 in front of it. then i'm trying to add a 1 to the very beginning of the string and then i'm trying to make it a large number that I can apply math to it.
I've tried a lot of different coding techniques. The closest I've gotten is below:
s = 'Ak'
for c in s:
mgk = (''.join(str(ord(c)) for c in s))
num = [mgk]
var = 1
num.insert(0, var)
mgc = lambda num: int(''.join(str(i) for i in num))
num = mgc(num)
print(num)
With this code I get the output: 165107
It's almost doing exactly what I need to do but it's taking out the 0 from the ord(A) which is 65. I want it to be 165. everything else seems to be working great. I'm using '%03d'% to insert the 0.
How I want it to work is:
Get the ord() value from a string of numbers and letters.
if the ord() value is less than 100 (ex: A = 65, add a 0 to make it a 3 digit number)
take the ord() values and combine them into 1 number. 0 needs to stay in from of 65. then add a one to the list. so basically the output will look like:
1065107
I want to make sure I can take that number and apply math to it.
I have this code too:
s = 'Ak'
for c in s:
s = ord(c)
s = '%03d'%s
mgk = (''.join(str(s)))
s = [mgk]
var = 1
s.insert(0, var)
mgc = lambda s: int(''.join(str(i) for i in s))
s = mgc(s)
print(s)
but then it counts each letter as its own element and it will not combine them and I only want the one in front of the very first number.
When the number is converted to an integer, it
Is this what you want? I am kinda confused:
a = 'Ak'
result = '1' + ''.join(str(f'{ord(char):03d}') for char in a)
print(result) # 1065107
# to make it a number just do:
my_int = int(result)

for some input in python3 in increasing order of list not come right

my code is:
n=int(input())
list_1 = []
for i in range(n):
list_1.append(input())
list_2=[]
#print(list_1)
while list_1:
minimum = list_1[0]
for x in list_1:
if x < minimum:
minimum = x
list_2.append(minimum)
list_1.remove(minimum)
print (' '.join(map(str, list_2)))
all output come correct but incorrect come in some input like
4
10
3
7
6
please help
Your list 'list_1' is a list of strings, and for strings minimums work in a different manner. For example, '10' < '3' is True.
Change the line:
list_1.append(input())
To:
list_1.append(int(input()))
The first thing you should do when posting questions here is properly explain your problem, and what the code does.
Now for your question, Mono found the issue in your code, but you should know that you do not need all this to sort a list of numbers. It already exists in the language. Use the sort() function on the list, like this:
print("This script will ask you for numbers and print them back to you in order.")
print("Enter how many numbers you will input: ", end="")
n=int(input())
list_1 = []
print("Please type each number.")
for i in range(n):
print(" Number", i, ": ", end='')
list_1.append(int(input()))
list_1.sort()
print("These are your numbers, in order:")
print (' '.join(map(str, list_1)))
The output is:
This script will ask you for numbers and print them back to you in order.
Enter how many numbers you will input: 4
Please type each number.
Number 0 : 10
Number 1 : 2
Number 2 : 8
Number 3 : 3
These are your numbers, in order:
2 3 8 10

couting a set of word in python

Assume s is a string of lower case characters.
Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then your program should print
Number of times bob occurs is: 2
This is my answer, but i dont know what's wrong with my code. Please help
s = "azcbobobegghakl"
coutBob=0
i=0
for char in range (len(s)):
if char[i:i+3]=="bob":
coutBob+=1
else:
i=i+1
print ("Number of times bob occurs is: " + str(coutBob))
You need to subscript the string s, not the index:
for i in range(len(s)):
if s[i:i+3]=="bob":
coutBob+=1
I think this will help you.
b = list(s)
i = 0
j = 0
for i in range(0,len(b)-2):
if b[i]=='b' and b[i+1]=='o' and b[i+2]=='b':
j = j + 1
print ("Number of times bob occurs is: %d"%j)

How to do a backspace in python

I'm trying to figure out how to print a one line string while using a for loop. If there are other ways that you know of, I would appreciate the help. Thank you. Also, try edit off my code!
times = int(input("Enter a number: "))
print(times)
a = 0
for i in range(times+1):
print("*"*i)
a += i
print("Total stars: ")
print(a)
print("Equation: ")
for e in range(1,times+1):
print(e)
if e != times:
print("+")
else:
pass
Out:
Enter a number: 5
*
**
***
****
*****
Equation:
1
+
2
+
3
+
4
+
5
How do I make the equation in just one single line like this:
1+2+3+4+5
I don't think you can do a "backspace" after you've printed. At least erasing from the terminal isn't going to be done very easily. But you can build the string before you print it:
times = int(input("Enter a number: "))
print(times)
a = 0
for i in range(times+1):
print("*"*i)
a += i
print("Total stars: ")
print(a)
print("Equation: ")
equation_string = ""
for e in range(1,times+1):
equation_string += str(e)
if e != times:
equation_string += "+"
else:
pass
print(equation_string)
Basically, what happens is you store the temporary equation in equation_str so it's built like this:
1
1+
1+2
1+2+
...
And then you print equation_str once it's completely built. The output of the modified program is this
Enter a number: 5
5
*
**
***
****
*****
Total stars:
15
Equation:
1+2+3+4+5
Feel free to post a comment if anything is unclear.
Instead of your original for loop to print each number, try this:
output = '+'.join([str(i) for i in range(1, times + 1)])
print(output)
Explanation:
[str(i) for i in range(1, times + 1)] is a list comprehension that returns a list of all your numbers, converted to strings so that we can print them.
'+'.join(...) joins each element of your list, with a + in between each element.
Alternatively:
If you want a simple modification to your original code, you can simply suppress the newline from each print statement with the keyword paramater end, and set this to an empty string:
print(e, end='')
(Note that I am addressed the implied question, not the 'how do I do a backspace' question)
Too long for a comment, so I will post here.
The formatting options of python can come into good use, if you have a sequence you wish to format and print.
Consider the following...
>>> num = 5 # number of numbers to generate
>>> n = num-1 # one less used in generating format string
>>> times = [i for i in range(1,num+1)] # generate your numbers
>>> ("{}+"*n + "{}=").format(*times) # format your outputs
'1+2+3+4+5='
So although this doesn't answer your question, you can see that list comprehensions can be brought into play to generate your list of values, which can then be used in the format generation. The format string can also be produced with a l.c. but it gets pretty messy when you want to incorporate string elements like the + and = as shown in the above example.
I think you are looking for the end parameter for the print function - i.e. print(e, end='') which prints each value of e as it arrives followed by no space or newline.

Return number of alphabetical substrings within input string

I'm trying to generate code to return the number of substrings within an input that are in sequential alphabetical order.
i.e. Input: 'abccbaabccba'
Output: 2
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def cake(x):
for i in range(len(x)):
for j in range (len(x)+1):
s = x[i:j+1]
l = 0
if s in alphabet:
l += 1
return l
print (cake('abccbaabccba'))
So far my code will only return 1. Based on tests I've done on it, it seems it just returns a 1 if there are letters in the input. Does anyone see where I'm going wrong?
You are getting the output 1 every time because your code resets the count to l = 0 on every pass through the loop.
If you fix this, you will get the answer 96, because you are including a lot of redundant checks on empty strings ('' in alphabet returns True).
If you fix that, you will get 17, because your test string contains substrings of length 1 and 2, as well as 3+, that are also substrings of the alphabet. So, your code needs to take into account the minimum substring length you would like to consider—which I assume is 3:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def cake(x, minLength=3):
l = 0
for i in range(len(x)):
for j in range(i+minLength, len(x)): # carefully specify both the start and end values of the loop that determines where your substring will end
s = x[i:j]
if s in alphabet:
print(repr(s))
l += 1
return l
print (cake('abccbaabccba'))

Resources