There is an lstrip method just for that:

str.lstrip([chars])

Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed.

'/test/test.json'.lstrip('/')

Ouput:

'test/test.json'
Answer from Thierry Lathuille on Stack Overflow
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ python-replace-first-occurrence-in-string-exampleexample.html
Python Replace First Occurrence in String Example - ItSolutionstuff.com
October 30, 2023 - In this examples, i will add myString variable with hello string. Then we will use replace() function to replace first occurrence from string in python.
Discussions

regex - Replace first occurrence of string in Python - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python Replace String in json output
I am working on an project where I shall get json results with HTTP request and send it to ElasticSiem. For doing this I will have results separated by new line The results I get are in this form > {"logsourcetypename_โ€ฆ More on discuss.python.org
๐ŸŒ discuss.python.org
1
0
February 26, 2022
python - Replace all occurrences of a string in JSON object regardless of key - Stack Overflow
I need to replace any occurrence of the string "N/A" with "Not Applicable" anywhere it appears regardless of its key or location before I upload the JSON to S3. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - replacing only the first occurance of a character in string - Stack Overflow
My objective is to replace the 0th string element with another string element. I tried the below code. ... While the 0th element does get replaced, the change is also reflected on the 3rd element. ... The replace function by default is replacing all the occurrences of 1 in the string. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ python-replace-first-character-occurrence-in-string-exampleexample.html
Python Replace First Character Occurrence in String Example - ItSolutionstuff.com
October 30, 2023 - In this example, i will add myString variable with hello string. Then we will use replace() function to replace first character occurrence from string in python.
๐ŸŒ
YouTube
youtube.com โ€บ the python oracle
Replace first occurrence of string in Python - YouTube
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn--Music by Eric Matyashttps://www.soundimage.orgTrack title: Popsicle...
Published: March 28, 2023
Views: 16
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ replace-first-occurrence-of-string-in-python.html
Replace first occurrence of string in Python
To replace the first occurrence of a substring within a string in Python, you can use the str.replace() method.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Python Replace String in json output - Python Help - Discussions on Python.org
February 26, 2022 - I am working on an project where I shall get json results with HTTP request and send it to ElasticSiem. For doing this I will have results separated by new line The results I get are in this form > {"logsourcetypename_deviceType": "Stonesoft Management Center", "High > Level Category": "Access"}, {"logsourcetypename_deviceType": > "Stonesoft Management Center", "High Level Category": "Access"}, But what I must have is > {"logsourcetypename_deviceType": "Stonesoft Management Center", "High > ...
Find elsewhere
Top answer
1 of 3
7

As mentioned in the comments, obj is a dict. One way to replace N/A with Not Applicable regardless of location is to convert it to a string, use string.replace and convert it back to dict for further processing

import json

#Original dict with N/A
obj = {'id': 'fab779b7-2586-4895-9f3b-c9518f34e028', 'project_id': 'a1a73e68-9943-4584-9d59-cc84a0d3e92b', 'created_at': '2017-10-23 02:57:03 -0700', 'sections': [{'section_name': '', 'items': [{'id': 'ffadc652-dd36-4b9f-817c-6539a4b462ab', 'created_at': '2017-10-23 03:36:13 -0700', 'updated_at': '2017-10-23 03:38:32 -0700', 'created_by': 'paul', 'question_text': 'Drawing Ref(s)', 'spec_ref': '', 'display_number': None, 'response': '', 'comment': 'see attached mh309', 'position': 1, 'is_conforming': 'N/A', 'display_type': 'text'}]}]}

#Convert to string and replace
obj_str = json.dumps(obj).replace('N/A', 'Not Applicable')

#Get obj back with replacement
obj = json.loads(obj_str)
2 of 3
4

Although @Devesh Kumar Singh's answer works with the sample json data in your question, converting the whole thing to a string, and then doing a wholesale bulk replace of the substring seems possibly error-prone because potentially it might change it in portions other than only in the values associated with dictionary keys.

To avoid that I would suggest using the following, which is more selective even though it takes a few more lines of code:

import json

def replace_NA(obj):

    def decode_dict(a_dict):
        for key, value in a_dict.items():
            try:
                a_dict[key] = value.replace('N/A', 'Not Applicable')
            except AttributeError:
                pass
        return a_dict

    return json.loads(json.dumps(obj), object_hook=decode_dict)


obj = {'id': 'fab779b7-2586-4895-9f3b-c9518f34e028', 'project_id': 'a1a73e68-9943-4584-9d59-cc84a0d3e92b', 'created_at': '2017-10-23 02:57:03 -0700', 'sections': [{'section_name': '', 'items': [{'id': 'ffadc652-dd36-4b9f-817c-6539a4b462ab', 'created_at': '2017-10-23 03:36:13 -0700', 'updated_at': '2017-10-23 03:38:32 -0700', 'created_by': 'paul', 'question_text': 'Drawing Ref(s)', 'spec_ref': '', 'display_number': None, 'response': '', 'comment': 'see attached mh309', 'position': 1, 'is_conforming': 'N/A', 'display_type': 'text'}]}]}
obj = replace_NA(obj)
๐ŸŒ
StudyMite
studymite.com โ€บ python โ€บ find-and-replace-first-occurrence-of-a-string-in-python
Find and Replace First Occurrence Of a String in python | StudyMite
March 2, 2023 - Another option is to use the find() method to locate the position of the first occurrence of the word or phrase, and then use slicing and concatenation to create a new string with the replacement word or phrase.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_replace.asp
Python String replace() Method
The replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified. ... txt = "one one was a race horse, two two was one too." x = ...
๐ŸŒ
Dirask
dirask.com โ€บ posts โ€บ Python-replace-first-character-in-string-1xyYqD
Python - replace first character in string
In this article, we would like to show you how to replace first character in string in Python. Quick solution: Practical example using replace() method In this ...
๐ŸŒ
Medium
parvez1487.medium.com โ€บ find-and-replace-first-occurrence-of-a-string-in-python-pythonpip-com-432fafdd21aa
Find and Replace First Occurrence of a String in Python โ€” pythonpip.com | by Parvez Alam | Medium
January 7, 2024 - This find and replace is a very common operation in web development. Here, Weโ€™ll discuss different approaches to find and replace the first occurrence of a substring in a string using Python.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python replace the first character in string | example code
Python replace the first character in string | Example code
June 8, 2023 - Use the Python built-in replace() function to replace the first character in the string. The str.replace takes 3 parameters old, new, and count (optional). Where count indicates the number of times you want to replace the old substring with ...