-
Notifications
You must be signed in to change notification settings - Fork 3
String & Slicing of Collection
String refers to text. Internally it is a collection of characters. just like list of characters or array of characters (just like other languages).
Strings are ordered sequences of character data. Indexing allows you to access individual characters in a string directly by using a numeric value. String indexing is zero-based: the first character in the string has index 0, the next is 1, and so on.
String in python is stored internally as below and characters can be accessed using the indexes.
Below, we can see the slicing can be done using syntax [:] ,[::]
Just like String, other collections can be sliced the same way. Here we have discussed all the ways we could slice a collection. Try using the slicing with string and list on your own as well.
Strings are Immutable
Strings are immutable in Python just like in c# or Java programming language.
text="tutorial"
text[0]="T"
# this will give error as we cannot change strings. Rather we can assign a new string to this variable text
text ="Tutorial"
# this is possible
text= text.replace("t","T")
# replace function which used old string and produces new string and returns it. this is possible
Keep Learning. Never Settle! 😊
- Home
- 1.Introduction To Python
- 2. DataTypes
- 3. Strings and Slicing of collection
- 4.If else & Loops
- 5. Break, continue, pass
- 6. Functions
- 7. Modules
- 8. Clarifying basics
- 9. File Handling
- 10. Exception Handling
- 11. Dig into Collections
- 12. Iterators, Generators and list comprehension
- 13. lambdas , map, filter and other concepts
- 14. args, kwargs an default parameters
- 15. Functional Programming
- 16. OOPs