Printing all string index locations of a specific character

python find all indexes of character in string
python string index
python find all occurrences in string
how to print certain letters from a string in python
python string substring
how to print every other letter in a string python
write a python program to print the index of the character in a string
remove character from string python

If I have a string e.g. s = '1084271054', and want to print all index locations of 0 e.g:

0 - 1, 7

What would be the most basic way to go about this?

[i for i, c in enumerate(s) if c=='0']
Out: [1, 7]

Python, Given a string and a character, your task is to find the first position of the character in the string. else : print ( "Character {} is present at {}" . format (c, str (​res)))  Print indexes of a character where particular characters of a string exists in C programming language, this is a string program for practice, here we will read a string and print the character’s indexes.

Maybe something like

>>> idx=[i for i in range(len(s)) if s[i]=='0']
>>> idx
[1, 7]

Edit:

>>> print ', '.join(str(i) for i in idx)
1, 7

How To Index and Slice Strings in Python 3, Index numbers allow us to access specific characters within a string. To print a substring that starts in the middle of a string and prints to the end, because the character “a” occurs at the -4 index number position, and the  When we refer to a particular index number of a string, Python returns the character that is in that position. Since the letter y is at index number 4 of the string ss = "Sammy Shark!", when we print ss[4] we receive y as the output. Index numbers allow us to access specific characters within a string. Accessing Characters by Negative Index Number

Pass the string into the enumerate function which returns an iterable of the indexes and characters. Then you can iterate over this and check if the character is '0' and if it is, print the index:

for i,c in enumerate(s):
    if c == '0':
        print(i)

which outputs:

1
7

If you want them on one line separated by commas, then you can use str.join with a generator:

print(', '.join(str(i) for i,c in enumerate(s) if c == '0'))

which gives:

1, 7

Python String index(), The index() method returns the index of a substring inside the string (if found). If the substring is not found, it raises an exception. print("Substring 'is fun':", result​). result = sentence.index('Java'). print("Substring 'Java':", result). Write a Python program to print the index of the character in a string. The following tool visualize what the computer is doing step-by-step as it executes the said program: Customize visualization ( NEW!) There was a problem connecting to the server. Please check your connection and try running the trinket again.

Getting Characters by Index from a String or String Buffer, You can get the character at a particular index within a string, string buffer, or string The substring method has two versions, as shown in the following table:​  As list.index () returns the index of first occurrence of an item in list. So, to find other occurrences of item in list, we will call list.index () repeatedly with range arguments. We have created a function that uses list.index () and returns a list of indexes of all occurrences of an item in given list i.e.

Python: Print the index of the character in a string, str1 = "w3resource" for index, char in enumerate(str1): print("Current character", char, "position at", index ). Sample Output: Current character w  The index parameter of the Chars[Int32] property is zero-based. Robust Programming. The Chars[Int32] property returns the character at the specified position. However, some Unicode characters can be represented by more than one character. For more information on how to work with Unicode characters, see How to: Convert a String to an Array of

Strings and Character Data in Python – Real Python, Given a numeric value n , chr(n) returns a string representing the character that String indexing in Python is zero-based: the first character in the string has index 0 will return a substring that is n - m characters in length, in this case, 5 - 2 = 3 . You can do this with a straightforward print() statement, separating numeric  print(str+2); Because str+2 gives you a pointer two elements along from the address pointed to by string. To get the last 3 characters, you need to obviously first know how long the string is.

Comments
  • SO is not a coding service. Show some of your work in regards to solving this problem and you might get some help.
  • Possible duplicate of python - find char in string - can I get all indexes?
  • Sorry I'm a nub
  • This seems good mate, is there any way to take out those pesky [ ] from the numbers?