Python: use substrings - how it works
In this practical tip, we explain how you can easily use substrings in Python. Because programming with Python is not that difficult.
Use substrings in Python
A substring is a string that is part of another (or larger) one. In Python, this method is called "slicing".
- First of all you have to store a string with a variable. Our example is: x = "Hello World!"
- With the command "x [2:]" you can now output the string without the first two digits: "llo World!"
- With the command "x [: 2]" in turn only the first two digits are output: "Hey"
- The "x [: - 2]" command, which outputs the string without the last two digits, is also very practical: "Hello Worl"
- This also works the other way round, so that only the last two digits are output: "d!"
- Finally, you can also combine the commands. The command "x [2: -2]" outputs the string without the first and last two digits, for example: "llo Worl"
More tips about Python in our CHIP guides
Tip: You can find more programming tutorials on our Python programming language topic page. There we will show you, for example, how you can program a professional GUI.