Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python get position of character in string

foo = "abcdefc"

# Using find()
foo.find('c')
# Output: 2
foo.find('g')
# Output: -1

# Using index()
# foo.index() is like foo.find(),
# But when the substring is not found, it raises an exception.
foo.index('h')
# ValueError: substring not found
Comment

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

python position letter in string

string = "This is a string"
position_of_letter_a = string.find('s')
    #output is 8, because the strings starts counting from 0 from left to right
position_from_the_right = string.rfind('s')
    #output is 10, because the strings starts counting from 0 and goes to the last 's' in the string
Comment

PREVIOUS NEXT
Code Example
Python :: django sort descending 
Python :: python how to get current line number 
Python :: split list in half python 
Python :: how to check if a letter is lowercase in python 
Python :: sklearn train_test_split 
Python :: pandas check if value in column is in a list 
Python :: how to replace first line of a textfile python 
Python :: python falsy values 
Python :: pytest check exception 
Python :: check if string has digits python 
Python :: python numba 
Python :: apostrophe in python 
Python :: select certain element from ndarray python 
Python :: python 3.9 features 
Python :: pandas sort 
Python :: catch error python 
Python :: python insert 
Python :: python restart script 
Python :: convert dict to dataframe 
Python :: How to convert simple string in to camel case in python 
Python :: pandas xlsx to dataframe 
Python :: python currency symbol 
Python :: python os open notepad 
Python :: jupyter notebook delete the output 
Python :: boto3 paginate 
Python :: pandas number of columns 
Python :: enumerate vs zip python same time 
Python :: change directory in python script 
Python :: python find all elements of substring in string 
Python :: python turtle clear screen 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =