Skip to main content

Python - String manipulation

To learn basic string manipulations in Python, you can try the examples below. I hope you find this useful.

Reminder: If you are an expert in Python then you can skip over article and still stay tuned for more articles on software engineering.

strings-example.py
___________________________________________________
#!/usr/bin/python
def main():
    s = 'this is a string'

    print(s.capitalize())
    print(s.title())
    print(s.upper())
    print(s.swapcase())
    print(s.find('is'))
    print(s.replace('this', 'that'))
    print(s.strip())
    print(s.isalnum())
    print(s.isalpha())
    print(s.isdigit())
    print(s.startswith("this"))
    pos = s.find('str', 1, 15)
    if pos == -1:
        print("did NOT find it")
    elif pos == 10:
        print ("found it at 10")
    else:
        print("found it")

    print("++++++++++++++++++++++++")

    str = "Hello World!"    print(str[2:])   # llo World!

    print(str[2:5])    # llo

    print(str[:7])     # Hello W

    print(str[:-2])    # Hello Worl

    print(str[-2:])    # d!

    print(str[2:-2])   # llo Worl

    print(str[::-1])   # this reverses the whole string

    print(str[::2])    # this skips characters going forward: HloWrd



if __name__ == "__main__": main()
___________________________________________________



Output:
___________________________________________________
This is a string
This Is A String
THIS IS A STRING
THIS IS A STRING
2
that is a string
this is a string
False
False
False
True
found it at 10
++++++++++++++++++++++++
llo World!
llo
Hello W
Hello Worl
d!
llo Worl
!dlroW olleH
HloWrd

___________________________________________________


Thank you for reading this post.

Almir Mustafic

Comments

Popular posts from this blog

Brand New programming language and one solution OR …

Brand New programming language and one solution OR Two existing programming languages, one solution for EACH? I understand that there is no right or wrong. It all depends on your software architecture, team structure, team skills and other factors, but I still want to explain the scenario as it may look familiar to some. Let me explain. Let’s assume that you have microservices and common libraries in two major programming languages. You have some teams who are experts in one and some teams experts in the other programming language. Now you need to come up with a solution for a scenario that all teams will need to leverage. Let’s assume that your cloud platform has an off-the-shelf approach for this but it is supported by a 3rd programming language that your teams do not have much experience in. What is the right thing for your organization and not just from the technical point of view? A) Do you embrace what your cloud platform gives you off the shelf and implement thi...

AMAZON WORKSPACES

AMAZON WORKSPACES. I am not sure how many of you have heard of Amazon WorkSpaces. Let me give you some information and my analysis after using it for some time. What is it? It is the Amazon’s solution to desktops in the cloud. What’s the point? Well, if you want to use a basic less powerful laptop or even use a Chromebook as your daily computer, then you will not be missing the full functionality of a powerful Windows 10 machine. For example, you can use your Chromebook to connect to your Amazon WorkSpace machine and then you get the full Windows 10 experience. The point is that it gives you the experience as if your remote/workspace Windows 10 machine is really part of your Chromebook. Where it makes the most sense is using Chromebooks to connect to your Amazon WorkSpace machine as Chromebooks with Chrome OS are fast for anything you do in the browser and they are very secure; Chromebooks are also generally cheaper than other laptops. Just a thought as an alternative to buying ful...

POCs ??

Imagine the world of software architecture where every POC becomes a production software without changes. That’s one extreme. Now imagine the opposite where no POCs get done and no free innovation happens and everything has to be 110% planned and build perfect from day one. If you and your team can take the good aspects of both extremes and combine into one nice approach, then you will innovate and that innovation will turn into enterprise worthy software. Thank you for reading. Almir Mustafic