Good day. I need help from those who know about my problem. How to get the total length of a string. I am trying to code that will get a total length of a string. However, if there one or more that have the same value, then it will not be counted.

def count():
     str1 = "abcdeb";

    print "String is : "+str(str1);
    print "Length is : ", len(str1);

I want to have an output of:

String is : abcdeb
Length is : 5;

since, there are 2 values of "b", then the other b is not counted. How will I do that?. I can't configure it. Thank you again in advance.

Convert the string into a list of characters. Convert the list into a set whose members are unique characters in the list. Then print the length of this set.

>>> str1 = "abcdeb";
>>> str1
'abcdeb'
>>> charset = set(list(str1))
>>> len(charset)
5

Thank you very much. It is a great help. I have been solving it for how many hours. Thank you. Thank you. ^_^

First, Python doesn't use semicolons to terminate lines of code.

P.S. Someone beat me to it, but maybe I can help a little more.

A fairly quick way to accomplish what you wish to accomplish is to create a set of the string and then check the length of that.

A set is a container for unique, hashable objects. (Strings are hashable because they are immutable, but hashability and mutability are really tangent to this discussion.)

Typecasting a string to a set will automatically filter out duplicate characters with three caveats. First, it won't preserve the order of the characters as they were in the original string. Second, lowercase letters and uppercase letters are represented using different values meaning a set won't filter out a capital 'T' if there is a lowercase 't' or vice versa. Third, it will also count whitespace meaning if it has spaces the set will have one space or if it has newlines the set will have one newline.

However, the first doesn't appear to be an issue for you and the second can be remedied using a string's 'lower' method. The third can be remedied by using a string's 'replace' method to replace any whitespace with a no character of two single quotes.

I'll give you an example using code like yours.

from string import whitespace

def char_items(string, count_caps = False, count_whitespace = False):
    if not count_caps:
        string = string.lower()
    if not count_whitespace:
        """
        Inefficient.
        Strings are immutable so replacing each whitespace character takes time and memory to create a new string.
        May create a more efficient algorithm later.
        """
        for character in whitespace:
            string = string.replace(character, '')
    return len(set(string))

mystring = 'abcdeb'

print "String is :", mystring
print "String composed of", char_items(mystring), "unique characters."

If you ever need to learn about objects and functions, you can use these codes in the interpreter to obtain information.

#Returns a list of the built in names and functions in Python.
dir(__builtins__)

#Prints help about an object. Where 'dir' can be replaced by the name of any object without quotes.
print(help(dir))
commented: Good supplemental info. After reading your answer I see mine had an unnecessary step. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.