How to check a string contains 3 dots in python [duplicate] - python

This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 10 months ago.
eg: "ABC.sample.int.int01" like that, only want to check this string contains 3 dots.
Thanks

You can check it like this, with count attribute of str in python. If it is equal to 3, then your strings actually contains exactly 3 dots.
if 'ABC.sample.int.int01'.count('.') == 3:
...
else:
...

Related

Is there any way to count the number of times a string is present in another given string? [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
I tried to create a program which returns the number of times a certain string occurs in the main string.
main_string="ABCDCDC"
find_string="CDC"
print(main_string.count(find_string))
Output=1
....
But there are 2 CDC. Is there any other ways to solve?
Try using regex:
print(len(re.findall(fr"(?={find_string})", main_string)))
Or try using this list comprehension:
x = len(find_string)
print(len([main_string[i:x + i] for i in range(len(main_string)) if main_string[i:x + i] == find_string]))
Both codes output:
2

How to split a number string using a comma in every 3rd digit from the right [duplicate]

This question already has answers here:
How to print a number using commas as thousands separators
(30 answers)
Add commas into number string [duplicate]
(11 answers)
What's the easiest way to add commas to an integer? [duplicate]
(4 answers)
Adding thousand separator while printing a number [duplicate]
(2 answers)
Closed 2 years ago.
A beginner here! Can I ask how to split a number string in every 3rd digit from the right and put a comma in between and do it again.
>>>num = '12550'
how can I make that into this
>>>12,550
You can use this neat trick:
num = 123456789
print ("{:,.2f}".format(num))
which outputs:
123,456,789.00
If you don't want the decimal places just use:
print ("{:,}".format(num))

How to increment numaric values only of alphanumeric values [duplicate]

This question already has answers here:
How do I create an incrementing filename in Python?
(13 answers)
convert number to formatted string in python with prefix 0s
(3 answers)
Closed 2 years ago.
proId = 'nam001'
I want to increment numeric values only. That should be like this 'nam002', 'nam003'.
How to do this?
Just because you asked nicely :D
You can use the simplest for loop, with str.zfill to pad with zeroes, then add it to the 'nam' prefix like this:
for i in range(1,11):
proId = 'nam' + str(i).zfill(3)
print(proId) # or do whatever you need with it...
Output:
nam001
nam002
...
nam009
nam010

How to split a string into a list by skipping letters and ^ char [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
extract digits in a simple way from a python string [duplicate]
(5 answers)
Closed 3 years ago.
I have the following string:
string_a = 81^A55
from which I'm trying to get the following list
string_a_int = [81,55]
I'm able to split the string into a list made by numbers as follows
list_only_number = re.split('[A-Z]+', string_a)
list_only_numbers = [81^, 55]
but I'm trying to figure out how to skip also the ^.
Any help would be much appreciated.

Is str.count() wrong? [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
How String.count() works? [duplicate]
(2 answers)
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
I'm having fun with some challenges and one of them makes me count substrings in a string. I have a problem specifically with "banana":
str = "banana"
print(str.count("ana"))
This should return 2 because "ana" appears two times:
b a n a n a
a n a
a n a
But str.count("ana") returns only 1. I've also tried with regexp:
import re
str = "banana"
print(len(re.findall("ana", str)))
But it also returns 1. Am I missing something?
thank you!
Yes, you are missing something.
str.count(): Return the number of (non-overlapping) occurrences of substring sub in string s

Categories