Python Forum
How to get the position of character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the position of character
#1
Hi,

I have two strings
str1 = 'ABCdef'
str2 = 'ABCdaf'
now I want to know which position (character) is different.
Desired output is 4
in str1: e
same position is str2: a
Reply
#2
If you want to know where the 'e' is, you use index: st1.index('e'). If you want to know what is at that location you use slicing: st1[4].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I don't know before hand,
1. check is two strings equal or not
2. If not equal. then get which position is not equal
Reply
#4
What have you tried?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Sep-16-2019, 12:40 PM)SriRajesh Wrote: I don't know before hand,
1. check is two strings equal or not
2. If not equal. then get which position is not equal

This is not reasonable approach. Why should you process strings twice? When are strings not equal? Strings are not equal if their corresponding characters don't match. So you can in one iteration compare corresponding characters and get indices of non-equal ones.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
str1= 'ABCdef'
str2= 'ABCdaf'
u=zip(str1,str2)
print (u)
for i,j in u:
    if i==j:
        print (i,'--',j)
    else: 
        print (i,'  ',j)
I want to get the positions where inequality
Reply
#7
(Sep-16-2019, 01:17 PM)SriRajesh Wrote: I want to get the positions where inequality

Maybe something along these lines:

>>> for i, pair in enumerate(zip('spam', 'eggs')):
...     print(i, pair)
... 
0 ('s', 'e')
1 ('p', 'g')
2 ('a', 'g')
3 ('m', 's')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,808 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 18,873 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,723 Apr-29-2022, 12:36 PM
Last Post: Armandito
  [solved] unexpected character after line continuation character paul18fr 4 3,396 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  python error: bad character range \|-t at position 12 faustineaiden 0 3,682 May-28-2021, 09:38 AM
Last Post: faustineaiden
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,172 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,735 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 6,192 Mar-25-2019, 12:54 PM
Last Post: silfer
  UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 23: c kapilan15 2 4,422 Jan-14-2019, 09:11 PM
Last Post: Gribouillis
  python charmap codec can't decode byte X in position Y character maps to < undefined> owais 9 39,117 Apr-28-2018, 10:52 PM
Last Post: abadawi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020