Python finds the position of a character in a string instance

Source: Internet
Author: User
Below for you to share a Python find characters in the string position instances, has a good reference value, I hope to be helpful to everyone. Come and see it together.

str_1= ' wo shi yi zhi da da niu ' char_1= ' i ' Npos=str_1.index (char_1) print (NPos)

Run Result: 7

======== is the use of find==========

str_1= ' wo shi yi zhi da da niu ' char_1= ' i ' Npos=str_1.find (char_1) print (NPos)

Results: 5

======== How do I find all the ' I ' positions in the string? ===========

#开挂模式str_1 = ' wo shi yi zhi da da niu ' char_1=str (input (' please input the char "you want: ')) Count=0str_list=list (str_1) for EA Ch_char in Str_list:count+=1 if each_char==char_1:  print (each_char,count-1)

Operation Result:

Please input the Char want:ii 0i 1i 2i 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.