Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 String slicing problem
#1
Hi, can someone please help me understand why this code doesn't work?

data = 'XBOX 360 | 150 | NEW'

product = data[data.index('1'):data.index('|')]
product
''
product = data[data.index('1'):data.index('N')]
product
'150 | '
As you can see it works when I index 'N' but not the '|'
Quote
#2
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> data = 'XBOX 360 | 150 | NEW'
>>> data.index('|')
9
>>> data.index('1')
11
It's because start index (11) is greater than end index (9)

what do you want to achieve?
Quote
#3
First of all, thank you Buran for your fast answer.

I want to slice out 150 without using the index number like this:

product = data[data.index('1'):14]
product
'150'
Quote
#4
you can split data at ' | '
>>> data = 'XBOX 360 | 150 | NEW'
>>> data.split(' | ')[1]
'150'
there are also other options, as well as it is possible to use RegEx
Quote
#5
That solved the problem!

Thank you very much, I appreciate the help!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Unicode string index problem luoheng 6 225 Nov-23-2019, 03:04 PM
Last Post: luoheng
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 335 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  string slicing default value Uchikago 1 391 Jul-01-2019, 11:19 AM
Last Post: perfringo
  string slicing Uchikago 2 359 Jun-28-2019, 06:35 AM
Last Post: perfringo
  simple string & input problem kungshamji 5 452 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  String slicing in python from reverse ift38375 1 406 Apr-29-2019, 06:58 AM
Last Post: perfringo
  String Slicing in List Comphrensions Patroclus72790 1 405 Mar-21-2019, 09:33 PM
Last Post: nilamo
  Problem with inserting a string in to Sqlite db darktitan 3 615 Mar-03-2019, 06:30 PM
Last Post: stranac
  IP string manipulation problem TheRealNoob 12 1,098 Feb-04-2019, 09:29 AM
Last Post: perfringo
  problem in replace string into file Saeid_Bibak 1 480 Jan-20-2019, 09:46 PM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)