Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Python Strings, indexing

Posted on 2011-09-15
5
Medium Priority
?
375 Views
Last Modified: 2012-06-27
I am trying to print certain parts of a string.
I think i need to use indexing like print(string[0:midChar] but its not working

error:

Traceback (most recent call last):
  File "C:/Python27/joe", line 6, in <module>
    print str[0:midChar]
TypeError: slice indices must be integers or None or have an __index__ method
0
Comment
Question by:Stephen1991
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
-Richard- earned 80 total points
ID: 36543436
print str[0:len(str)/2]
midChar is the actual midde character of the string, and therefore is of type character.  It is not the index of the middle character, which would be an integer, which is required by the bracket notation for indexing into strings.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 36543468
Remember though, the second value in

stringName[ start:finish ]

identifies the first character that won't be returned...

For example:
>>> stringName = '0123456789'
>>> stringName[ 2:5 ]
'234'
>>>

Open in new window

0
 

Author Comment

by:Stephen1991
ID: 36543482
Okay. Thank you very much.


to print the rest of the string after the middle is this okay:

print word[(len(word)/2)+1:]
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 36543495
yup
>>> word = 'Now is the time for all good men to come to the aid of their country.'
>>> print word[(len(word)/2)+1:]
 come to the aid of their country.
>>>

Open in new window

0
 

Author Comment

by:Stephen1991
ID: 36543532
Nicee.  Thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

647 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question