Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
Previous Tutorial Next TutorialBackpack Color Bag Shoulder Bag Travel YSMYWM School Holographic Daypack Laser Women Satchel Candy Backpack Transparent UB74zH
Strings are bits of text. They can be defined as anything between quotes:
- script.py
- Wallet Coin Canvas Butterfly Purse Wallet Ladies Handbag Zipper Purses Cartoon Iron Cat Small Women HqFvAf
- Banquet Deep Vintage Evening Handmade Phone Bags Bag Female Party Clutch Purse Blue v8qqFtnBw
- Plots
As you can see, the first thing you learned was printing a simple sentence. This sentence was stored by Python as a string. However, instead of immediately printing strings out, we will explore the various things you can do to them. You can also use single quotes to assign a string. However, you will face problems if the value to be assigned itself contains single quotes.For example to assign the string in these bracket(single quotes are ' ') you need to use double quotes only like this
- script.py
- Wallet Coin Canvas Butterfly Purse Wallet Ladies Handbag Zipper Purses Cartoon Iron Cat Small Women HqFvAf
- Banquet Deep Vintage Evening Handmade Phone Bags Bag Female Party Clutch Purse Blue v8qqFtnBw
- Plots
That prints out 12, because "Hello world!" is 12 characters long, including punctuation and spaces.
- script.py
- Wallet Coin Canvas Butterfly Purse Wallet Ladies Handbag Zipper Purses Cartoon Iron Cat Small Women HqFvAf
- Banquet Deep Vintage Evening Handmade Phone Bags Bag Female Party Clutch Purse Blue v8qqFtnBw
- Plots
- Handbag Backpack Deep Suede Purse Leather Coffee uJHRXvj2dUe Bag Leisure Nico Shoulder Casual Tassel Genuine Women xSqAw7wa
- Crossbody York Darian Pebbled Mulberry Bag Street Leather Shoulder New Kate Black Spade XqnE88
That prints out 4, because the location of the first occurrence of the letter "o" is 4 characters away from the first character. Notice how there are actually two o's in the phrase - this method only recognizes the first.
But why didn't it print out 5? Isn't "o" the fifth character in the string? To make things more simple, Python (and most other programming languages) start things at 0 instead of 1. So the index of "o" is 4.
For those of you using silly fonts, that is a lowercase L, not a number one. This counts the number of l's in the string. Therefore, it should print 3.
- script.py
- Wallet Coin Canvas Butterfly Purse Wallet Ladies Handbag Zipper Purses Cartoon Iron Cat Small Women HqFvAf
- Banquet Deep Vintage Evening Handmade Phone Bags Bag Female Party Clutch Purse Blue v8qqFtnBw
- Plots
- Handbag Backpack Deep Suede Purse Leather Coffee uJHRXvj2dUe Bag Leisure Nico Shoulder Casual Tassel Genuine Women xSqAw7wa
- Crossbody York Darian Pebbled Mulberry Bag Street Leather Shoulder New Kate Black Spade XqnE88
This prints a slice of the string, starting at index 3, and ending at index 6. But why 6 and not 7? Again, most programming languages do this - it makes doing math inside those brackets easier.
If you just have one number in the brackets, it will give you the single character at that index. If you leave out the first number but keep the colon, it will give you a slice from the start to the number you left in. If you leave out the second number, if will give you a slice from the first number to the end.
You can even put negative numbers inside the brackets. They are an easy way of starting at the end of the string instead of the beginning. This way, -3 means "3rd character from the end".
- script.py
- Wallet Coin Canvas Butterfly Purse Wallet Ladies Handbag Zipper Purses Cartoon Iron Cat Small Women HqFvAf
- Banquet Deep Vintage Evening Handmade Phone Bags Bag Female Party Clutch Purse Blue v8qqFtnBw
- Plots
This prints the characters of string from 3 to 7 skipping one character. This is extended slice syntax. The general form is [start:stop:step].
- script.py
- Wallet Coin Canvas Butterfly Purse Wallet Ladies Handbag Zipper Purses Cartoon Iron Cat Small Women HqFvAf
- Banquet Deep Vintage Evening Handmade Phone Bags Bag Female Party Clutch Purse Blue v8qqFtnBw
- Plots
- Handbag Backpack Deep Suede Purse Leather Coffee uJHRXvj2dUe Bag Leisure Nico Shoulder Casual Tassel Genuine Women xSqAw7wa Adjustable 35" Style Straps Guitar Arrow Handbags Myathle Wide Bag Canvas Crossbody 2" Colorful Strap 51" Colored Purse Replacement for nBBvf6wzq
- Crossbody York Darian Pebbled Mulberry Bag Street Leather Shoulder New Kate Black Spade XqnE88
Note that both of them produce same output
There is no function like strrev in C to reverse a string. But with the above mentioned type of slice syntax you can easily reverse a string like this
This
- script.py
- Wallet Coin Canvas Butterfly Purse Wallet Ladies Handbag Zipper Purses Cartoon Iron Cat Small Women HqFvAf
- Banquet Deep Vintage Evening Handmade Phone Bags Bag Female Party Clutch Purse Blue v8qqFtnBw
- Plots
These make a new string with all letters converted to uppercase and lowercase, respectively.
This is used to determine whether the string starts with something or ends with something, respectively. The first one will print True, as the string starts with "Hello". The second one will print False, as the string certainly does not end with "asdfasdfasdf".
This splits the string into a bunch of strings grouped together in a list. Since this example splits at a space, the first item in the list will be "Hello", and the second will be "world!".
Exercise
Try to fix the code to print out the correct information by changing the string.
- script.py
- Wallet Coin Canvas Butterfly Purse Wallet Ladies Handbag Zipper Purses Cartoon Iron Cat Small Women HqFvAf
- Banquet Deep Vintage Evening Handmade Phone Bags Bag Female Party Clutch Purse Blue v8qqFtnBw
- Plots
- Handbag Backpack Deep Suede Purse Leather Coffee uJHRXvj2dUe Bag Leisure Nico Shoulder Casual Tassel Genuine Women xSqAw7wa
- Crossbody York Darian Pebbled Mulberry Bag Street Leather Shoulder New Kate Black Spade XqnE88
This site generously supported by Shoulder Messenger Women Purse Bags Donalworld Leather Pt1 Handbag Bag Tote Pu xZd8T7wq. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!
Previous Tutorial Next Tutorial