Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

find the index of a character in a string python

my_var = 'mummy'. #Find the position of 'm'

#Using find - find returns the index for the first instance from the left.
my_var.find('m')
# Output: 0

#Using rfind - rfind returns the index for the first instance from the right.
my_var.rfind('m')
# Output: 3
# With find() and rfind(), when substring is not found, it returns -1.

#NB: You can use index() and rindex(). In this case, when the substring is not
# found, it raises an exception.
Comment

find charechtar index in string python

myString = 'Position of a character'
myString.find('s')
# 2
myString.find('x')
# -1
Comment

how to get all index of a char of a string in python

word = 'Hello'
to_find = 'l'

# in one line
print([i for i, x in enumerate(word) if x == to_find])
Comment

PREVIOUS NEXT
Code Example
Python :: read ms word with python 
Python :: Example Layout using grid() in tkinter 
Python :: python try else 
Python :: python type hinting pandas dataframe 
Python :: how to return number in binary python 
Python :: how to append list in python 
Python :: replace characters in string python 
Python :: python one line if without else 
Python :: skip to next iteration python 
Python :: how to make a dice program in python 
Python :: python txt to parquet 
Python :: python PyDrive service account credentials 
Python :: python for web development 
Python :: python input character limit 
Python :: Splitting strings in Python without split() 
Python :: get the invite url of server disc.py 
Python :: swapping in python 
Python :: list to dataframe columns 
Python :: np matrix drop zero column 
Python :: def python 
Python :: panda python 
Python :: get method in python 
Python :: how to parse timestamp in python 
Python :: get all keys and values from dictionary python 
Python :: loginrequiredmixin django 
Python :: how to print in double quotes in python 
Python :: how to check if character in string python 
Python :: open and write in a file in python 
Python :: pickling python example 
Python :: python list of dictionaries to list 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =