Power button issues are common for a variety of iPhone models, and it isn’t the actual button that is usually the problem. Rather, the flex cable that sits underneath the power button gets worn down and too thin and that is normally when power buttons stop working correctly. This is not something you can easily fix on your own. Your best bet is if your iPhone is still under warranty, just get this issue repaired as it is covered under normal use circumstances.
If your iPhone isn’t under warranty, the best thing to do is go to Settings, General, Accessibility and then click “Assistive Touch”. Slide the toggle to ON and you will see a grey square with a white circle appear. You can move this little square anywhere on your screen that is most convenient for you. This square is an icon that will give you a variety of options that you can choose from including Siri or Voice Control, Device, Home and Favorites. When you tap on Device you will see “Lock Phone”. You can use this to turn the iPhone off. The only way to get your iPhone back on from this point is to connect it to your charger or USB port though, so you might want to have a charger handy for this reason.