SoFunction
Updated on 2024-11-14

Example of a method to count the number of identical characters in python

descriptive Write a program that accepts a string of letters, numbers, and spaces, and a letter, and then outputs the number of occurrences of that letter in the input string. It is case insensitive and the length of the string is less than 500.

Enter a description:

Enter a string of letters and numbers and spaces on the first line and a letter on the second line.

Output Description:

Outputs the number of characters in the input string that contain the character.

Example 1

Input:

ABCabc
A

Output:

2

//code1
def ge_num():
    fir_line = input()
    sec_line = input()
    if len(sec_line) == 0 or len(sec_line) >1:
        return "Fill in the second line with one character:"
    leng = len(fir_line.strip().lower().split(sec_line.lower()))-1

    return leng
print(ge_num())
// code2
a=input().lower()
b=input().lower()
print((b))

Knowledge Points:

1, Python string strip () method

The strip() method in Python is used to remove a specified character (space or newline by default) or sequence of characters at the beginning and end of a string.
Note: This method can only delete the beginning or the end of the character, not the middle part of the character.

Its function prototype:(s[, chars]), which returns a copy of the string with the leading and trailing characters removed. (Meaning you want to remove which characters inside the string, then you pass those characters as arguments. This function will only remove the leading and trailing characters, not the ones in the middle.)

If the argument to strip() is null, then whitespace characters at the beginning and end of the string (including \n, \r, \t, and so on) will be removed by default.

  • lstrip(): remove the left side
  • rstrip(): remove right side

parameters

chars - Removes the sequence of characters specified at the beginning and end of the string.

return value

Returns a new string created by removing the characters specified at the beginning and end of the string.

an actual example

str = “00000003210Runoob01230000000”;
print ( ‘0' ); # Remove first and last characters 0

str2 = " Runoob ";
print ();# Remove leading and trailing spaces

The output of the above example is as follows:

3210Runoob0123
Runoob

From the result, you can notice that the characters in the middle part are not deleted.

The following example demonstrates deleting a character from a specified character sequence as long as the header and footer contain the character:

Example:

str = “123abcrunoob321”
print (( ‘12' )) # The character sequence is 12

The output of the above example is as follows:

3abcrunoob3

Cite link.Python strip() method.

2, Python string split () method

Python split() slices a string by specifying a delimiter, separating num+1 substrings if the argument num has a specified value.

The syntax of the split() method:

(str="", num=(str)).

###str - A separator character that defaults to all null characters, including spaces, newlines (\n), tabs (\t), and so on.
###num - The number of times to split. Defaults to -1, which means all.

Cite link.Python split() method.

3, Python string lower () method

Python lower() methodConverts all uppercase characters in a string to lowercase.

lower() method syntax:

()

Return Value:Returns the string generated after converting all uppercase characters in the string to lowercase.

an actual example

str = “THIS IS STRING EXAMPLE…WOW!!!”;
print ();

The output of the above example is as follows:

this is string example…wow!!!

Supplementary: python counts the maximum number of consecutively identical characters in two strings starting from the first character

The function to count the maximum number of consecutive identical characters from the first character of two strings in python is as follows:

def get_num(s1, s2):
    num = 0
    len_s1 = len(s1)
    list_s1 = []
    for i in range(len_s1):
        two_s1 = s1[0:i+1]
        list_s1.append(two_s1)
    for i in list_s1:
        if (i) and len(i) > num:
            num = len(i)
    return num

summarize

to this article on the python statistics on the number of the same characters to this article, more related python statistics on the number of the same characters content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!