Solved

Python Strings, indexing

Posted on 2011-09-15
5
356 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
  • 2
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
-Richard- earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Nicee.  Thanks.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python Exploit Script 7 101
Python output problem 10 49
Trying to run a Python File 11 58
Python 2.7 - Passing arguments 8 46
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now