Counting the number of continuous palindromic substrings Given a string of lowercase ASCII characters, find all distinct continuous palindromic sub-strings of it.
Here is my code:

def palindrome(s):
    if s==s[::-1]:
        return True


noTest=int(input())
s=[]
for _ in range(noTest):
    s.append(input())

count=[]
for word in s:
    letters=[]
    psub=[]
    for l in word:
        letters.append(l)
        if l not in psub:
            psub.append(l)

    for i in range(len(letters)):
        for k in range(i+1,len(letters)+1):
             sub=('').join(letters[i:k])
             #print (i,k,sub)
             if palindrome(sub) and sub not in psub:
                 psub.append(sub)

    count.append(len(psub))

for c in count:
    print (c)  


Input:
The first line of input contains an integer T denoting the number of test cases.    Then T test cases follow. Each test case contains a string.  

Output:
Print the count of distinct continuous palindromic sub-strings of it.
First off, you should organize your code by using functions as it will help you simplify it and is better for reusability and testing. As I see it, you can use a function to generate the various substrings of the word, and a function to count the amount of palindromes in a string. You could also organize the remaining top-level code in a main() function and guard it with if __name__ == '__main__'::

def palindrome(s):
    if s==s[::-1]:
        return True


def substrings(letters):
    subs = []
    for i in range(len(letters)):
        for k in range(i+1,len(letters)+1):
             sub=('').join(letters[i:k])
             subs.append(sub)
    return subs


def count_palindromes(word):
    letters=[]
    psub=[]
    for l in word:
        letters.append(l)
        if l not in psub:
            psub.append(l)

    for sub in substrings(letters):
         if palindrome(sub) and sub not in psub:
             psub.append(sub)

    return len(psub)


def main():
    noTest=int(input())
    s=[]
    for _ in range(noTest):
        s.append(input())

    count=[]
    for word in s:
        count.append(count_palindromes(word))

    for c in count:
        print (c)


if __name__ == '__main__':
    main()


Now we can start tidying things up. For starter, apply PEP8 to make it look like other Python code and simplify main() as we don't need all those intermediate lists anymore:

def main():
    tests_count = int(input())
    for _ in range(tests_count):
        print(count_palindromes(input()))


Apply the same kind of improvements to the palindrome function:

def is_palindrome(string):
    return string == string[::-1]


Second, use a better datastructure than lists in count_palindromes as checking l not in psub or sub not in psub is an \$\mathcal{O}(n)\$ operation compared to \$\mathcal{O}(1)\$ for the same kind of check in a set. Besides, sets guarantee that each element added in is only added once, so you can get rid of these checks yourself.

def count_palindromes(word):
    palindromes = set(word)
    for sub in substrings(word):
        if is_palindrome(sub):
            palindromes.add(sub)
    return len(palindromes)


Note that I passed the whole word to substrings instead of a list of letters, as len or slicing ([i:k]) would work on both. Also, you could use the filter builtin instead of an explicit check which will allow you to write:

def count_palindromes(word):
    palindromes = set(word)
    palindromes.update(filter(is_palindrome, substrings(word)))
    return len(palindromes)


Lastly, you can optimize substring generation by:


getting rid of the join call as you already have strings when slicing a string;
using a generator rather than building a list and appending to it.




def substrings(word):
    word_length = len(word)
    for i in range(word_length):
        for k in range(i + 1, word_length + 1):
            yield word[i:k]


Full code:

def substrings(word):
    word_length = len(word)
    for i in range(word_length):
        for k in range(i + 1, word_length + 1):
            yield word[i:k]


def is_palindrome(string):
    return string == string[::-1]


def count_palindromes(word):
    palindromes = set(word)
    palindromes.update(filter(is_palindrome, substrings(word)))
    return len(palindromes)


def main():
    tests_count = int(input())
    for _ in range(tests_count):
        print(count_palindromes(input()))


if __name__ == '__main__':
    main()




You could also use the itertools module and adapt a bit the pairwise recipe to make substrings a bit more efficient:

import itertools


def substrings(string, size):
    iterators = itertools.tee(string, size)
    substrings_generator = (
            itertools.islice(i, start, None)
            for start, i in enumerate(iterators)
    )
    return map(''.join, zip(*substrings_generator))


def is_palindrome(string):
    return string == string[::-1]


def count_palindromes(word):
    palindromes = set(word)
    for sub_size in range(2, len(word) + 1):
        palindromes.update(filter(is_palindrome, substrings(word, sub_size)))
    return len(palindromes)


def main():
    tests_count = int(input())
    for _ in range(tests_count):
        print(count_palindromes(input()))


if __name__ == '__main__':
    main()